new instantiation design alternatives

Dmitry Lomov dslomov at chromium.org
Thu Sep 18 13:12:52 PDT 2014


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

> From: dslomov at google.com [mailto:dslomov at google.com] On Behalf Of Dmitry
> Lomov
>
> > I am not sure what do you mean by "abstract" class here.
>
> I'm just quoting Allen's gist. It's a base class whose constructor always
> throws (in the gist, by doing `this = undefined`). Almost all DOM classes
> are currently like this, for example.
>

Got it.


>
> > 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.
>
> My argument for it is that it works exactly like normal function-based
> inheritance works today. That is:
>
> ```js
> function Base() { }
> Base.prototype.foo = function () { };
>
> function Derived() {
>   // note: no Base.call(this)
> }
> Derived.prototype = Object.create(Base.prototype);
>
> var d = new Derived();
> assert(typeof d.foo === "function");
> ```
>
> Despite omitting the `Base.call(this)`, which is analogous to omitting
> `this = new super()`, I still have a correctly set-up prototype linkage. I
> would prefer if the following ES6 code:
>
> ```js
> class Base { foo() { } }
>
> class Derived extends Base {
>   constructor() { /* note: no this = new super() */ }
> }
>
> var d = new Derived();
> assert(typeof d.foo === "function");
> ```
>
> also had the assertion pass. With alternative 1 (no implicit super; no
> implicit `this = Object.create(new^.prototype)`), the assertion will fail,
> as the prototype linkage will be incorrect.
>

Note that in "no explicit constructor invocation" case you will fail
earlier (on return from Derived constructor), and this is an early error.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20140918/210a36ff/attachment.html>


More information about the es-discuss mailing list