Allen Wirfs-Brock allen at wirfs-brock.com
Tue Jun 17 16:08:10 PDT 2014

On Jun 17, 2014, at 1:02 PM, Jason Orendorff wrote:

> On Tue, Jun 17, 2014 at 2:49 PM, Jasper St. Pierre
> <jstpierre at mecheye.net> wrote:
>> How would
>>    constructor() {
>>        if (rand() > 0.5)
>>            super("A");
>>    }
>> behave?
> SyntaxError.
>> We could prevent this behavior by making sure that super(); must be the
>> first statement in a constructor, [...]
> That is what I proposed.

We discussed issues like here early in the ES6 class design and the consensus at the time was that we didn't want to be restricted to a super first design.

Also, it's not clear what it really buys you because if you can say:

>   class RocketPack extends PowerUp {
>        constructor() {
>            super("Rocket Pack", 1000);
>            this.fuel = FULL_TANK;
>        }
>    }

You can presumably also say:

  class RocketPack extends PowerUp {
       constructor() {
           super(this.foo(), doSomethingWith(this));
           this.fuel = FULL_TANK;

so you still have to deal with the issue of the meaning of 'this' before and after the super call in

> The RocketPack constructor would behave like this:
>        static [Symbol.new]() {
>            var obj = super[Symbol.new]("Rocket Pack", 1000);
>            obj.fuel = FULL_TANK;
>            return obj;
>        }

which reminds me that @@new is a class-side method, not an instance method. So, its natural 'this' value is the class object and and access to that 'this' value is important (for example, accessing the 'prototype' property in the default @@new, access class side constants and methods, etc.).

I guess, Jason is arguing that if you need to do any of these things, you need to explicitly code a @@new such as

class RocketPack extends PowerUp {
       static [Symbol.new]() {
           let obj = super(this.foo(), doSomethingWith(this)); //this is normally RocketPack or a subclass of it
           obj.fuel = FULL_TANK;

BTW, it's not clear to me in this case what gets called for

Related, if an @@new method is explicitly provided in a class definition, is a constructor method definition forbidden?


More information about the es-discuss mailing list