Subclassing ES6 objects with ES5 syntax.

Andrea Giammarchi andrea.giammarchi at gmail.com
Fri Apr 24 14:46:41 UTC 2015


So you should do the same with Promise methods but then you'll see overall
a quite consistent performance drop when using all these subclasses.

We've been trying for about 10 years now (
http://dean.edwards.name/weblog/2006/11/hooray/ ) and yet we don't have a
working and performing solution, which I guess is why somebody still
pollutes global prototypes, everything becomes easier, including the
ability to still use common literals to define objects or lists.

Oh well :-)

Best Regards

On Fri, Apr 24, 2015 at 3:26 PM, Alex Kocharin <alex at kocharin.ru> wrote:

>
> True, but it should be your responsibility (as a person who implements a
> subclass) to resolve those issues.
>
> Thankfully, it's easily done:
>
> ```
> function MyArray(...args) {
>   var self = Array.apply(null, args)
>   Object.setPrototypeOf(self, MyArray.prototype)
>   return self
> }
>
> Object.setPrototypeOf(MyArray.prototype, Array.prototype)
>
> ;[ 'slice', 'map', 'concat' /* whatever else */ ].forEach(function
> (method) {
>   MyArray.prototype[method] = function (...args) {
>     var result = Array.prototype[method].apply(this, args)
>     Object.setPrototypeOf(result, MyArray.prototype)
>     return result
>   }
> })
>
> console.log(MyArray(1).slice(1) instanceof MyArray)
> // true
> ```
>
>
> 24.04.2015, 14:13, "Andrea Giammarchi" <andrea.giammarchi at gmail.com>:
>
> Not exactly ... if it's an Array, as example, the moment you
> slice/map/splice/concat, etc will return an instanceof Array, not an
> instance of whatever you have sublcassed.
>
> Regards
>
> On Fri, Apr 24, 2015 at 10:24 AM, Alex Kocharin <alex at kocharin.ru> wrote:
>
>
> I believe you can subclass anything using code like this:
>
> function MyPromise(executor) {
>   var self = new Promise(executor)
>   self.setPrototypeOf(self, MyPromise.prototype)
>   return self
> }
> Object.setPrototypeOf(MyPromise, Promise)
>
>
> ... and it can be easily subclassed itself in the same way.
>
>
> 24.04.2015, 04:02, "C. Scott Ananian" <ecmascript at cscott.net>:
>
> 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
> 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/20150424/b0e4cdf0/attachment.html>


More information about the es-discuss mailing list