primitive boxing in instanceof due to Symbol.hasInstance
allen at wirfs-brock.com
Tue Jun 16 01:43:19 UTC 2015
On Jun 15, 2015, at 10:06 AM, Bradley Meck wrote:
> Kyle Simpson caused the discovery of this oddity :
> https://people.mozilla.org/~jorendorff/es6-draft.html#sec-instanceofoperator calls GetMethod on C without checking if it is an Object, this means `1 instanceof Foo` could be affected by mutating `Number.prototype[Symbol.hasInstance]`.
> I am assuming this is unintended?
No, it is exactly as intended:
GetMethod users GetV http://people.mozilla.org/~jorendorff/es6-draft.html#sec-getv which handles accessing properties for primitives. For primitive values, it uses the built-in wrapper prototype to lookup properties, but it does not actually create a boxed primitive value.
`this` object boxing is a characteristic of the callee, not the caller. Non-srict functions box primitive `this values` that are passed to them, strict mode functions do not.
This is all the standard method invocation behavior, and the `instanceof` operator just does a normal method invocation if it finds a `Symbol.hasInstance` method. Because Function.prototype implements `Symbol.hasInstacce` all function haves the default legacy behavior when appearing right-side operand of `instanceof`
But `instanceof` does not depend upon the existence of a a `Symbol.hasInstance` method. It falls back to performing the default (legacy) instanceof check if a callable `hasInstance` property value is not found.
But, back to the original question: Exactly what do you expect to be boxed for `1 instance of Foo`? The `C` that GetMethod is applied to is `Foo` which would normally be a constructor or some other "type-like" object implements. It could be a primitive numeric value (eg, `5 instanceof 4`) which will cause the fallback to be taken using Number.prototype has a `Symbol.hasInstance` callable property. But, even if it does why would you expect anything to box?
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the es-discuss