new instantiation design alternatives

Allen Wirfs-Brock allen at wirfs-brock.com
Mon Sep 15 11:42:06 PDT 2014


On Sep 14, 2014, at 7:47 PM, Domenic Denicola wrote:

> I want to give a +1 to Allen. The syntax really is what-you-see-is-what-you-get, which is a great virtue. The C++-derived initializer is cute and tempting, but not nearly as powerful or easy to grok.
> 
> That said, I feel weakly that requiring the correct `this = new super(...)`and/or `this = Object.create(new^.prototype)` invocation is a high price to pay for every derived class ever. In particular, "basic constructor functions" (ES5 "classes") that don't wish to use the superclass constructor don't need `this = Object.create(new^.prototype)`, so making ES6 classes require that is strange. It feels like protypal inheritance has "broken" once you start using ES6 classes, and it requires you to fix it by manually doing something that was previously implicit.
> 
> I think I'd most be in favor of a third option that implicitly adds `this = Object.create(new^.prototype)` if no `this`-assignment is present. That way, the superclass constructor is never implicitly called, which is kind of what you would expect. But if you do no `this =` assignment, things don't totally break: you still get your prototype and a valid `this`, but you don't inherit the allocation or initialization logic from the superclass. 

Thanks, that's a plausible third alternative to the two variations we already have.  I'll take a look at creating a gist for that alternative.

Since there has been a lot of talk about  verboseness of `this=` I want to compare several alternatives  for a derived class that wants to do it's own allocation: (this is derived from the last example in https://gist.github.com/allenwb/291035fbf910eab8e9a6 ):

If `this=` assignment is not allowed then any locally allocating method has to look something like this:

```js
   constructor(a) {
      let newO = new^.specialAllocationMethod(); //or Object.create(new^), etc.
      newO.a = a;
      return newO;
   }
```

with `this`` assignment it looks like:
```js
   constructor(a) {
     this = new^.specialAllocationMethod(); //or Object.create(new^), etc.
     this.a = a;
   }
```

Which is more verbose?

Allen




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


More information about the es-discuss mailing list