new instantiation design alternatives

Dmitry Lomov dslomov at chromium.org
Thu Sep 18 11:51:48 PDT 2014


On Thu, Sep 18, 2014 at 8:41 PM, Domenic Denicola <
domenic at domenicdenicola.com> wrote:

> 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};
> }
> ```
>

Ok, you are right, I stand corrected. However, the above cases are quite
exotic, to the point that I question the need to support them with class
syntax.
Still I agree that new^ is a fine device for expressing them.


>
> Or deriving from an abstract base class:
>
> ```js
> class D7 extends AbstractBase {
>   constructor () {
>     this = Object.create(new^.prototype); //instances are ordinary objects
>   }
> }
> ```
>

I am not sure what do you mean by "abstract" class here.


>
> 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.


I think "the Dominic alternative" is problematic in that it makes it easy
to forget to call base constructor. In majority of the cases you really
want to do it, and implicit "this = Objec.create(new^.prototype)" will do
the wrong thing without warning you.

Dmitry
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20140918/3edf8ffc/attachment.html>


More information about the es-discuss mailing list