Subclassing ES6 objects with ES5 syntax.

Caitlin Potter caitpotter88 at gmail.com
Fri Apr 24 01:25:56 UTC 2015


So, why exactly is SuperCall forbidden in function bodies? If people work around it with Reflect.construct anyways, it just seems nice to use SuperCall instead — at least if new.target is defined

> On Apr 23, 2015, at 9:24 PM, Erik Arvidsson <erik.arvidsson at gmail.com> wrote:
> 
> new.target is available in functions.
> 
> 
> On Thu, Apr 23, 2015, 21:02 C. Scott Ananian <ecmascript at cscott.net <mailto:ecmascript at cscott.net>> wrote:
> Is there any way to access `new.target` using ES5 syntax?
> 
> It appears that the "correct" way to create a subclass using ES5 syntax is:
> ```
> function MyPromise(executor) {
>   var self = Reflect.construct(Promise, [executor], new.target);
>   return self;
> }
> Object.setPrototypeOf(MyPromise, Promise);
> ```
> But since `new.target` isn't accessible, we have to do something like:
> ```
> function MyPromise(executor) {
>   var self = Reflect.construct(Promise, [executor], MyPromise); // <-- THIS
>   return self;
> }
> Object.setPrototypeOf(MyPromise, Promise);
> ```
> which works for only a single level of subclassing.  That is, it allows us to create and instantiate MyPromise, but now nobody can subclass MyPromise.  That's too bad.
> 
> Is there any way around this?
>   --scott
> 
> ps. Use case: My `prfun` package on npm subclasses `Promise` in order to add all the useful utility helpers without stomping on the global `Promise` object.  I'd like to do so in a way which is compatible with both native ES6 promises (if they are available) and properly-written ES5 shims.
> 
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org <mailto:es-discuss at mozilla.org>
> https://mail.mozilla.org/listinfo/es-discuss <https://mail.mozilla.org/listinfo/es-discuss>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20150423/92a3d297/attachment-0001.html>


More information about the es-discuss mailing list