new instantiation design alternatives

Claude Pache claude.pache at gmail.com
Wed Sep 17 03:51:53 PDT 2014


Le 15 sept. 2014 à 23:19, Kevin Smith <zenparsing at gmail.com> a écrit :

> 
> 
> Isn't the latter (since it specifies ": super(x)") actually identical to
> 
>     constructor(x, y) {
>         this = new super();
>         this.y = y;
>     }
> 
> IOW, isn't it "I am constructor only and will throw if {[Call]]ed)?
> 
> No.  The idea is that the "class create expression" is only called when the constructor is "new"d.  It's purpose is to set up the "this" value when [[Construct]] is called, exactly as @@create used to.
> 

When you write:

    constructor(x, y) {
        if (new^)
            this = new super(x);
        this.y = y;
    }

is it intentional that you call the super-constructor only when you have been called with `new`, and not in case of a direct call?

For instance, if I wanted to support to be called through the legacy `SuperConstructor.call(this, ...args)` trick in addition to be new’d, I'd rather try the following:

    constructor(x, y) {
        if (new^)
            this = new super(x);
        else
            super.constructor(x);
        this.y = y;
    }

The point here is that the purpose of the constructor method is not only allocation, but also (and primarily) initialisation.

—Claude


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20140917/f8ee763b/attachment-0001.html>


More information about the es-discuss mailing list