new instantiation design alternatives

Domenic Denicola domenic at domenicdenicola.com
Thu Sep 18 11:41:23 PDT 2014


From: es-discuss [mailto:es-discuss-bounces at mozilla.org] On Behalf Of Dmitry Lomov

> new^ is really just for 'constructors that are callable as functions as well'.

This is not really correct. Looking through Allen's gists, it's important in several scenarios. E.g. allocating an exotic object, but still getting the prototype linkage correct:

```js
class Base2 {
  constructor(x) {
    this = [ ];  //create a new exotic array instance
    Object.setPrototypeOf(this, new^.prototype);
    this[0] = x;
  }
  isBase2 () {return true}
}
```

Or ignoring the super-class constructor, but still getting the prototype linkage correct:

```js
class Derived4 extends Base2 {
   constructor (a) {
      this = Object.create(new^.prototype);
      this.a = a;
    }
  isDerived4() {return true};
}
```

Or deriving from an abstract base class:

```js
class D7 extends AbstractBase {
  constructor () {
    this = Object.create(new^.prototype); //instances are ordinary objects
  }
}
```

Note that if we adopted "the Domenic alternative", i.e. implicit `this = Object.create(new^.prototype)`, the latter two would no longer need the corresponding line in their constructor, leaving usage of `new^` for (a) different [[Call]] behavior and (b) exotic allocation.



More information about the es-discuss mailing list