Not forcing super in derived class's constructor?
Claude Pache
claude.pache at gmail.com
Mon May 11 17:08:33 UTC 2015
> Le 11 mai 2015 à 05:26, Glen Huang <curvedmark at gmail.com> a écrit :
>
> No one care to comment? Hope I didn't mistake how super works.
>
>> On May 8, 2015, at 11:05 AM, Glen Huang <curvedmark at gmail.com> wrote:
>>
>> TLDR: If `this` is used without `super` (which should be statically analyzable), let it refer to Object.create(new.target.prototype). Otherwise, let super creates what it refers to.
>>
>> I know the reason to force `super` in derived class's constructor is to make sure `this` refers to the exotic object the base class might allocate.
>>
>> But I bet in real world, extending base classes who only create ordinary objects is more common than extending those create exotic objects. And forget to call super is going to be frequent mistake. In es5, when you extend a class like this
>>
>> ```js
>> function Foo() {
>> Bar.call(this)
>> }
>> Foo.prototype = Object.create(Bar.prototype);
>> ```
>>
>> `this` refers to Object.create(new.target.prototype). So I wonder if we can keep this behavior, making things less surprising when people transit to es 2015?
Yes, in the course of refactoring your classes, you are indeed forced to transform your old clunky `Bar.call(this)` invocations into new shiny `super()` invocations, and it is IMHO a good thing. I don't see personally the point to support deprecated patterns in new syntactic forms.
But anyway, if refactoring your old classes is too difficult (for it might need more complicated changes that the aforementioned simple substitution), you are not obliged to: the old ES3/5 "classes" will continue to work, and will even be usable as superclass (in the `extends` clause) of new ES2015-classes.
—Claude
More information about the es-discuss
mailing list