Primitives vs. Objects, Ruby-like features

Brendan Eich brendan at mozilla.org
Mon Mar 24 20:12:12 PDT 2008


On Mar 24, 2008, at 7:43 PM, Darryl wrote:

> In current versions of JS there's some weird stuff
> where some primitives are equal to their object
> equivalents:
>
> 1 == new Number(1)

Use === for identity testing.

> But in other cases they're not equivalents at all:
>
> typeof(1) == "number"
> typeof(new Number(1)) == "object"

typeof does not convert object to its default value, as == does.

> And sometimes theres weird syntax errors:
>
> 5.prototype //parse error

As in almost all programming languages, the parser breaks up input  
into tokens and favors the longest token at each step. So 5. is a  
floating point number, short for 5.0. This is the same in Java, C, C+ 
+, etc.

Another example of longest token winning: i+++5, which is (i++) + 5.

> function N(n){ return n }
> N(5).prototype //== Number, wtf?

What did you expect? Same as 5..prototype or (5).prototype or  
(5.0).prototype or ....

The ship sailed on these long ago, almost 13 years since the birth of  
JS, over 12 years since Netscape 2, 11 years since ECMA-262 Edition 1.

/be




More information about the Es4-discuss mailing list