I noted some open issues on "Classes with Trait Composition"

David Herman dherman at mozilla.com
Wed May 18 11:42:44 PDT 2011


> Using "new" for the constructor is one of my favorite feature's of Allen's proposal. Things I like about it:
> 
> 1. It's terse. Since almost every class defines a ctor, this is helpful. "constructor" is a mouthful and repeating the full class name (like in Java, C++, etc.) is redundant.
> 2. I think it's pretty clear to a user what's going on.
> 3. It avoids stealing a valid identifier. You can't define a method named "new" but "constructor" is a valid property name.

Yes you can:

    var obj = {
        new: function() { print("hey what's up") }
    }

To my mind, this slightly weakens the case for |new| over |constructor|. You'd be special-casing the syntax *not* to create a property called "new" even though in object literals it does.

But I'm somewhat divided about this particular issue. You're right that the longer name hurts both for writing and reading. In both cases, the name is being special-cased (in either case, there's something special going on: the function defines the call and construct behavior of the class's constructor function, and both the instance *and* the prototype are given a property called |constructor|). But arguably there's something slightly less surprising about giving the special-case semantics to a reserved word, and of course only in the context of a class body. And yet, as I said above, there is something slightly weird about *not* creating a property called "new" when using the keyword |new|. (I think it would be a bad idea to have it create *both* a property called |constructor| *and* a property called |new|.)

Dave



More information about the es-discuss mailing list