primitive boxing in instanceof due to Symbol.hasInstance

Allen Wirfs-Brock allen at
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  

`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.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the es-discuss mailing list