why not "new" instead of "constructor"?

Axel Rauschmayer axel at rauschma.de
Sat Nov 19 09:54:24 PST 2011


> Why can't we just use "new" in such declarations:
> 
> let Point = class {
>    x: 0,  //not really needed unless defining an object exemplar 
>    y: 0, 
>    new(x,y) {
>       this.x = x;
>       this.y=y;
>    }
> };
>    
> It is shorter to type, reads better, and is much more suggestive of its direct relationship with the new operator.
> 
> The only real issue I can think of is that in ES5 unquoted "new" is a valid property name in an object literal.  However, in this case we are using it in a new syntactic context (a concise method property) that doesn't exist in ES5.  Whether or not we also let gave new this meaning in a propertyAssignment form (a breaking change) can be a secondary design decision to be considered.  Related would be concern that there are frameworks that actually use obj.new() as an idiom.  We probably would need to be careful to not break those, but I think that is also doable.  
> 
> There are various semantic details that need to be specified, but none of them seem difficult.  Before getting into them, I'm more interested in what is essentially a bikesheding questions:  is this a direction that we shoud consider?

I like the idea of replacing the method name “constructor” with something better, but “new” suggests instantiation *and* initialization to me. Is “init” a possibility?

-- 
Dr. Axel Rauschmayer
axel at rauschma.de

home: rauschma.de
twitter: twitter.com/rauschma
blog: 2ality.com



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20111119/549b7d0b/attachment.html>


More information about the es-discuss mailing list