I entered this statement in JSLint:
var number = new Number(3);
And received the following message:
Do not use Number as a constructor.
Why is that? The statement is creating a number object, not a primitive value, so I don't see why using new
is a problem.
EDIT: Thanks for all the responses. They've got me thinking further, so I posted a follow-up question here.
UPDATE: Thanks again for all the responses! They've been very helpful. I guess to sum up, in JavaScript, an Object type is not equal to another Object type, even when they have the exact same value, unless they are both the EXACT SAME object.
In other words, in Matthew's example below, n1 == n2 is false because you are comparing two REFERENCES to two SEPARATE objects, but n1 == n1 is true because you are comparing references to the EXACT SAME object.
So, while I now understand why using Number as a constructor can cause problems, I found you can use the valueOf property when comparing Number objects.
In other words, n1.valueOf == n2.valueOf is true! (This is because you're comparing the return values of the valueOf FUNCTION, not the REFERENCES to the objects themselves.)