Determine if a value is Callable/Constructible

Claude Pache claude.pache at gmail.com
Mon Mar 30 10:23:02 UTC 2015


> Le 30 mars 2015 à 10:46, Allen Wirfs-Brock <allen at wirfs-brock.com> a écrit :
> 
>> 
>> On Mar 30, 2015, at 10:12 AM, Axel Rauschmayer <axel at rauschma.de> wrote:
>> 
>>>> It doesn’t seem that big of a deal, but one risk is: people mistaking a class for a constructor, trying to subclass it as if it were a constructor and things failing silently.
>>>> 
>>> Can you give an example of what you mean?
>> 
>> ```js
>> class MySuperClass {}
>> 
>> // This function assumes that MySuperClass is an ES5 constructor function
>> function MySubConstructor(foo) {
>>    MySuperClass.call(this);
>>    this.foo = foo;
>> }
>> MySubConstructor.prototype = Object.create(MySuperClass.prototype);
>> MySubConstructor.prototype.constructor = MySubConstructor;
>> ```
> 
> so if MySuperCall didn’t have the throw on [[Call]] behavior the above would work just fine.
> 
> Allen

I see an issue when MySuperClass contains itself a super() invocation... at least when that super-class is some builtin that doesn't support initialisation of pre-allocated instances, like `Array`. As currently specified, it will just throw, which is at least safe. It would be interesting to make it just work without hacks such as `if (new.target) super(); else super.constructor(...);`, and, in the same time, without silently break with `Array`, etc.

—Claude
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20150330/43e9ec48/attachment.html>


More information about the es-discuss mailing list