On 8/22/2013 8:04 AM, Mark S. Miller wrote:
var Promise = (function(){
"use strict"; // of course
var brand = new WeakMap();
// only ever called with "new"
function HiddenPromiseConstructor(callback) {
// initialize "this", which it can assume starts fresh and
trustworthily uninitialized
brand.set(this, true);
}
function Promise(arg) {
if (Object.getPrototypeOf(this) === Promise.prototype &&
!(brand.has(this))) {
// assume likely called with "new", but do not trust
"this"
return new HiddenPromiseConstructor(arg)
} else {
// assume called for coercion behavior. Ignore this
if (brand.has(arg)) {
return arg;
} else {
return Promise.of(arg);
}
}
}
HiddenPromiseConstructor.prototype = Promise.prototype;
// initialize Promise.prototype
// initialize Promise statics
return Promise
})();
I'd make one small change to this:
if (Object.getPrototypeOf(this) === Promise.prototype &&
!(brand.has(this))) {
to
if (this instanceof Promise && !(brand.has(this))) {
or an alternative that I think is functionally identical, if you want to
avoid instanceof:
if (Promise.prototype.isPrototypeOf(this) && !(brand.has(this))) {
This change would allow subclassing of Promise.
_______________________________________________
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss