primitive boxing in instanceof due to Symbol.hasInstance

Bradley Meck bradley.meck at gmail.com
Mon Jun 15 20:14:44 UTC 2015


Well this also has slightly odd behavior since the Object operation will
fail for OrdinaryHastInstance if for primitives
https://people.mozilla.org/~jorendorff/es6-draft.html#sec-ordinaryhasinstance

On Mon, Jun 15, 2015 at 3:09 PM, Andrea Giammarchi <
andrea.giammarchi at gmail.com> wrote:

> point of views I guess, you could fix forever the inconsistency via
>
> ```js
> Number.prototype[Symbol.hasInstance] = function () {
>   return typeof this.valueOf() === 'number';
> };
> ```
>
> and fix String, Boolean, Symbol too (why not) and whatever else.
> `instanceof` is the new `typeof` ^_^ (naa, just joking)
>
> Best Regards
>
>
>
> On Mon, Jun 15, 2015 at 7:39 PM, Bradley Meck <bradley.meck at gmail.com>
> wrote:
>
>> Yes, with regards to O being an Object.
>>
>> This seems a bit strange to me since things like:
>>
>> ```
>> Number.prototype.toString = function () {return 'overriden';}
>>
>> (1)+'' // "1"
>>
>>
>> String(1) // "1"
>>
>> (1).toString() // "overriden"
>> ```
>>
>> Implicit string coercion does not box the object.
>> String constructor does not box the object.
>>
>> Only the `.` operator which requires an Object boxes the object.
>>
>> `instanceof` works on non-objects but I think is the only case of an
>> operator working on both primitives and objects causing boxing.
>>
>> Is there a reason constructors could/should be allowed to override
>> `instanceof` for primitives? This just seems odd to have this cause boxing
>> while `1 instanceof Number` is `false`.
>>
>>
>> On Mon, Jun 15, 2015 at 12:20 PM, Kevin Smith <zenparsing at gmail.com>
>> wrote:
>>
>>>
>>>> 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 assume you mean "without checking if O is an object"
>>>
>>>
>>>> I am assuming this is unintended?
>>>>
>>>
>>> IIUC the point of @@hasInstance is to allow the programmer to define
>>> completely arbitrary semantics for "instanceof" when applied to "C", even
>>> if C is a built-in.  Do you see a problem with this behavior?
>>>
>>>
>>
>> _______________________________________________
>> es-discuss mailing list
>> es-discuss at mozilla.org
>> https://mail.mozilla.org/listinfo/es-discuss
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20150615/aecf71f7/attachment.html>


More information about the es-discuss mailing list