primitive boxing in instanceof due to Symbol.hasInstance

Allen Wirfs-Brock allen at wirfs-brock.com
Tue Jun 16 05:53:40 UTC 2015


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/20150615/1ab1eecf/attachment-0001.html>


More information about the es-discuss mailing list