new instantiation design alternatives

Rick Waldron waldron.rick at gmail.com
Mon Sep 15 10:17:30 PDT 2014


On Sun, Sep 14, 2014 at 10:47 PM, Domenic Denicola <
domenic at domenicdenicola.com> 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.
>

Based on the gists and everything presented in this thread, I'm also +1
here for the same reason Domenic points out. The intention of code that
reads: `this = new super();` should be very easily understood, based on my
own experience—I was confident in the semantics before I began reading the
explanations and rationale. That's not to say that I expect everyone to
have the same experience.



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


In ClassDefinitionEvaluation step 2 and 3 (
https://people.mozilla.org/~jorendorff/es6-draft.html#sec-runtime-semantics-classdefinitionevaluation)
this is already covered by the semantics for the `extends Superclass`
syntax.



> That way, the superclass constructor is never implicitly called, which is
> kind of what you would expect.


Which is how it works in the existing design:

  class Abstract {
    constructor() {
      this.a = "own instance property";
    }
    m() {
      return "method defined on Abstract.prototype";
    }
  }

  class Derived extends Abstract {
    constructor() {}
  }


  var d = new Derived();
  console.log(typeof d.a === "undefined"); // superclass constructor is
never implicitly called
  console.

  var d = new Derived();

  console.log(typeof d.a === "undefined"); // superclass constructor is
never implicitly called
  console.log(typeof d.m === "function");   // protoParent link is present
as expected.


This should be extended to subclass definitions with no constructor method:

  class Derived extends Abstract {}

  var d = new Derived();
  console.log(typeof d.a === "undefined"); // superclass constructor is
never implicitly called




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


More information about the es-discuss mailing list