domenic at domenicdenicola.com (2013-08-29T19:13:57.947Z)
On Thu, Aug 22, 2013 at 9:22 AM, Brandon Benvie <bbenvie at mozilla.com> wrote:
> I'd make one small change to this:
>
>
> ```js
> if (Object.getPrototypeOf(this) === Promise.prototype && !(brand.has(this))) {
> ```
>
> to
>
> ```js
> if (this instanceof Promise && !(brand.has(this))) {
> ```
>
> or an alternative that I think is functionally identical, if you want to
> avoid instanceof:
>
> ```js
> if (Promise.prototype.isPrototypeOf(this) && !(brand.has(this))) {
> ```
>
> This change would allow subclassing of Promise.
This change would mislead my example
Object.create(p, {Promise: {value: Promise}}).Promise(....)
into calling Promise's construct behavior instead of its coerce behavior.
On Thu, Aug 22, 2013 at 9:22 AM, Brandon Benvie <bbenvie at mozilla.com> wrote: > 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. This change would mislead my example Object.create(p, {Promise: {value: Promise}}).Promise(....) into calling Promise's construct behavior instead of its coerce behavior. > > > ______________________________**_________________ > es-discuss mailing list > es-discuss at mozilla.org > https://mail.mozilla.org/**listinfo/es-discuss<https://mail.mozilla.org/listinfo/es-discuss> > -- Cheers, --MarkM -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20130822/65e8ca0b/attachment.html>