new instantiation design alternatives

Domenic Denicola domenic at domenicdenicola.com
Thu Sep 18 13:06:36 PDT 2014


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



More information about the es-discuss mailing list