primitive boxing in instanceof due to Symbol.hasInstance

Bradley Meck bradley.meck at gmail.com
Tue Jun 16 13:04:08 UTC 2015


You are correct, it is not the primitive being boxed, but the primitive
being passed into @@hasInstance.

Still consider it very strange that I can change if `1 instanceof Number`
but if that was intentional /shrug. Typeof I guess will be the safe way to
detect things.

On Tue, Jun 16, 2015 at 12:53 AM, Allen Wirfs-Brock <allen at wirfs-brock.com>
wrote:

>
> On Jun 15, 2015, at 9:21 PM, Bradley Meck wrote:
>
> On the surface `O instanceof C` boxes `C`, but when using `GetMethod` O
> turns O into an Object. This means `instanceof` is temporarily turning O
> into an Object.
>
>
> Read the spec
> http://people.mozilla.org/~jorendorff/es6-draft.html#sec-relational-operators-runtime-semantics-evaluation
>
>
> `o instanceof C`  performs InstanceOfOperator(O,C) which performs
> GetMethod(C, @@hasInstance)
>
> so GetMethod doesn't even see the value of `o`.
>
>
> For primitives, all the operators in JS have been unable to be altered
> until this was pointed out. `1 instanceof C` is able to be changed by
> mutating %NumberPrototype% , `"" instanceof C` by mutating
> %StringPrototype% , etc.
>
>
> In ES6 certain operations (instanceof and the internal ToPrimitive which
> is used by several operator) were made extensible because Host and built-in
> objects have always been able to extend their semantics.  For example,
> WebIDL has it's own unique definition of instanceof that it uses for DOM
> objects.  Extensibility of these operations via JS code is necessary in
> order to enable such libraries to be directly self-hosted using JS code.
>
>
> Compared to other operators this is new and a bit surprising that
> primitive behaviors are mutable.
>
> I would expect this behavior by boxing primitives via `Object(primitive)
> instanceof C` but not for primitives.
>
>
> O is the argument to hasInstance methods.  JS has never auto-boxed
> primitive argument values.
>
> Allen
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20150616/777a355e/attachment.html>


More information about the es-discuss mailing list