Subclassing ES6 objects with ES5 syntax.

C. Scott Ananian ecmascript at
Fri Apr 24 15:31:03 UTC 2015

On Fri, Apr 24, 2015 at 10:46 AM, Andrea Giammarchi <
andrea.giammarchi at> wrote:

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

Well, not exactly.  The Promise methods are all written to use the correct
constructor (perhaps a (welcome) artifact of the fact that they were
spec'ed before this change in instantiation mechanism?):
$ iojs
> var P2 = function P2(x) { var self = new Promise(x);
Object.setPrototypeOf(self, P2.prototype); return self; }
> Object.setPrototypeOf(P2, Promise);
[Function: P2]
>   P2.prototype = Object.create(Promise.prototype);
>   P2.prototype.constructor = P2;
> p = P2.resolve(5);
> p instanceof Promise
> p instanceof P2
> p2 = p.then(function(){return 6; });
> p2 instanceof P2
It's true that the `Object.setPrototypeOf` inside the constructor is quite
unfortunate for performance.  :( But at least you don't have to repeat that
inside each instance method.

Does anyone have a better idea for creating an ES6-compatible subclass
using ES5 syntax, without resorting to `Object.setPrototypeOf` when every
instance is constructed?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the es-discuss mailing list