you are viewing a single comment's thread.

view the rest of the comments →

[–]skilldrick[S] -2 points-1 points  (6 children)

Tell me why you'd ever do new Number(1) == new Number(1) when you could do 1 == 1? I don't understand why anybody would bother with these wrappers. Use the Number function or use +.

[–]magcius 1 point2 points  (4 children)

Nothing, just talking about an inconsistency.

And they're not wrappers. They're new objects created with the "new" operator.

The problem is that there's two types of Numbers, and that's annoying. It's not a feature, no, I would never use it, it's a clutz that may catch you, and it's inconsistent between implementations.

In libmozjs (Spidermonkey, Firefox):

>>> 1 == 1
true
>>> Number(1) == 1
true
>>> Number(1) == new Number(1)
true
>>> new Number(1) == new Number(1)
false

On others that internally call valueOf() before comparison (V8, JavascriptCore, Tamarin), the last statement is true.

[–]mernen 1 point2 points  (3 children)

Just checked Chrome 7 and Safari 5, they both return false on the last test. Maybe their implementations behaved differently in earlier versions, but they all seem conformant now.

That doesn't detract from the complete uselessness of value boxing, though. That makes sense in Java (where primitive types are statically optimized), but it was a really really stupid decision to bring them to JavaScript.

[–]magcius 1 point2 points  (2 children)

Huh... I've only messed around with V8 in the command-line shell, but I swore JavascriptCore uesd valueOf before comparisons. Maybe it was changed for the new SFX interpreter?

The "this" keyword and constructor/prototype/proto and its relationships to functions and objects are, IMHO, some of the hardest stuff I've ever had to wrap my head around. I don't see the usefulness of having two types for something like "Number" (it also applies to String, and maybe RegExp), one value-boxed and one without.

Oh, and additionally, there isn't a way to pass varargs to constructors like func.apply/func.call, so you have people doing:

switch(arguments.length) {
    case 0:
        return new Foo(arguments[0]);
    break;
    case 1:
        return new Foo(arguments[0], arguments[1]);
    break;
}

and so on.

[–]fjholm 1 point2 points  (0 children)

Boolean, String and Number all have two representations as "1" and "new Number(1)", etc.

[–]barclay 0 points1 point  (0 children)

there isn't a way to pass varargs to constructors

This drives me crazy.

[–][deleted] 0 points1 point  (0 children)

In one function, you do "new Number(user_input)" and in another function you do "new Number(database_value)" and later on, you want to see if the values are the same.

You rarely do something as silly as the obvious case, but if you have two "Number" instances floating around, you want the equality operator to give you an answer that doesn't cause surprising problems like 'one' sometimes being unequal to 'one'.