instanceof Operator

Jeff Dyer jodyer at adobe.com
Sun Oct 21 11:48:51 PDT 2007




On 10/21/07 11:34 AM, Lars T Hansen wrote:

> On 10/21/07, Jeff Dyer <jodyer at adobe.com> wrote:
>> 
>> On 10/21/07 10:03 AM, liorean wrote:
>> 
>>> On 21/10/2007, Eugen.Konkov at aldec.com <Eugen.Konkov at aldec.com> wrote:
>>>> var a;
>>>> a= {};
>>>> a instanceof Object //true
>>>> a= [];
>>>> a instanceof Array //true
>>>> a='asdf';
>>>> a instanceof String //false
>>>> a= 7;
>>>> a instanceof Number //false
>>>> 
>>>> Why?
>>> 
>>> Because those are primitives of type double and string respectively.
>>> They are not instances of any of the compound types Object, String or
>>> Number.
>>> 
>>> Something silly that JavaScript inherited from Java that the world
>>> would be much better off without, but as I understand it won't be
>>> corrected because of real world compatibility problems.
>> 
>> This problem is fixed by the addition of the 'is' operator in ES4. Replace
>> 'instanceof' with 'is' in all of the above, and the result will be true in
>> each case. You correctly point out that 'instanceof' is terminally broken
>> for compatibility's sake.
> 
> I don't think "is" fixes it, because "string" is not a subclass of
> "String" (for compatibility reasons) and "abcd" is "string".  However,
> 
> "abcd" is string => true
> "abcd" instanceof string => true
> 
> The wrapper classes String, Number, Boolean are similar to the
> (primitive) values they wrap, but they're not really related to those
> value types in a type sense, and in ES4 the wrappers are of even less
> utility than in ES3, I would say.
> 

Right, thanks for the correction. This is an obvious consequence of the
recent return of primitive wrappers to ES4. 'string' is the new 'String'!

Jd




More information about the Es4-discuss mailing list