domenic at domenicdenicola.com (2014-02-24T21:08:03.356Z)
I'm not talking about the class system in general. I'm talking about
the ES5+ code:
```js
Promise.prototype.bind = function(newThis) {
var SuperPromise = this._bindSuper || this.constructor || Promise;
// create a new Promise subclass (this is less cumbersome in es6, sigh)
var BoundPromise = function(exec) {
return SuperPromise.call(this, exec);
};
Object.setPrototypeOf(BoundPromise, SuperPromise);
BoundPromise.prototype = Object.create(SuperPromise.prototype);
BoundPromise.prototype.constructor = BoundPromise;
BoundPromise.prototype._bindSuper = SuperPromise;
BoundPromise.prototype.then = (function(superThen) {
return function(f, r) {
var ff = f && f.bind(newThis);
var rr = r && r.bind(newThis);
return superThen.call(this, ff, rr);
};
})(BoundPromise.prototype.then);
return newThis ? BoundPromise.resolve(this) : SuperPromise.resolve(this);
};
```
which is already useful in practice. It would be nice if the browsers
would support Promise subclassing of this sort, in the (long?) interim
without ES6 class syntax.
On Sat, Feb 15, 2014 at 3:17 AM, Anne van Kesteren <annevk at annevk.nl> wrote: > It will take a long time before browsers support subclassing in > general as far as I can tell. I'm not talking about the class system in general. I'm talking about the ES5+ code: ```js Promise.prototype.bind = function(newThis) { var SuperPromise = this._bindSuper || this.constructor || Promise; // create a new Promise subclass (this is less cumbersome in es6, sigh) var BoundPromise = function(exec) { return SuperPromise.call(this, exec); }; Object.setPrototypeOf(BoundPromise, SuperPromise); BoundPromise.prototype = Object.create(SuperPromise.prototype); BoundPromise.prototype.constructor = BoundPromise; BoundPromise.prototype._bindSuper = SuperPromise; BoundPromise.prototype.then = (function(superThen) { return function(f, r) { var ff = f && f.bind(newThis); var rr = r && r.bind(newThis); return superThen.call(this, ff, rr); }; })(BoundPromise.prototype.then); return newThis ? BoundPromise.resolve(this) : SuperPromise.resolve(this); }; ``` which is already useful in practice. It would be nice if the browsers would support Promise subclassing of this sort, in the (long?) interim without ES6 class syntax. --scott