Why shouldn't you use Number as a constructor?

I introduced this statement in JSLint:

var number = new Number(3); 

And received the following message:

Do not use Number as a constructor.

Why? The operator creates a numerical object, not a primitive value, so I don’t understand why using new is a problem.

EDIT: Thanks for all the answers. They made me think, so I posted the next question here .

+20
javascript jslint constructor oop
Dec 15 '08 at 18:09
source share
6 answers

In addition to break === and typeof the returning "object", using the Number constructor also changes the way that values ​​are used in boolean contexts. Since "new Number (0)" is an object, not a literal value, it evaluates to "true" because it is not null. For example:

 var n1 = 0; var n2 = new Number(0); n1 == n2 // true n1 === n2 // false if (n1) { // Doesn't execute } if (n2) { // Does execute, because n2 is an object that is not null } 

Edit: Even worse than breaking === between numeric literals and Number objects, == doesn't even work between two Number objects (at least not in an intuitive way - they check for identity, not equality).

 var n1 = new Number(3); var n2 = new Number(3); alert(n1 == n2); // false alert(n1 === n2); // false 
+29
Dec 15 '08 at 19:31
source share
 var number = new Number(3); alert(typeof number); // gives "object" 

Creating the variable number is of type Object , probably not the most desirable result. Pay attention to:

 var number = Number(3); alert(typeof number); // gives "number" 
+13
Dec 15 '08 at 18:21
source share

new Number () does not return the same object as a numeric literal. This means using the new Number () breaks ===, which is the best way to check for exact equality in Javascript.

 >>> 3 == 1 + 2 true >>> 3 === 1 + 2 true >>> new Number(3) == 1 + 2 true >>> new Number(3) === 1 + 2 false 

You can find the rationale for JSLint in the author’s book, JavaScript: The Good Parts , in Appendix C.

+5
Dec 15 '08 at 18:28
source share

Unfortunately, JSLint docs do not go into details than "does not expect to see", so we remain guessing. My own suspicion is that this simplifies type checking:

 assert(typeof 3 === "number"); assert(typeof new Number(3) === "object"); 

If you mix two codes, your type checks become more complex:

 if (typeof foo === "number" || foo instanceof Number) { … } 

However, JSLint also comes across Object and Array constructors that do not make this distinction, so it could just be an authoring style in a coding style:

 assert(typeof [] === "object"); assert(typeof new Array() === "object"); assert(typeof {} === "object"); assert(typeof new Object() === "object"); 

Edit:. Stephen's answer gives a great point - the equality operator without expressions (===). Numeric objects and numerical primitives will never be considered equal to this operator, even if their values ​​are the same:

 assert(3 !== new Number(3)); 
+4
Dec 15 '08 at 18:22
source share

It is slower and requires more memory. Runtime can handle immutable literals as immutable literals. This means that when it occurs 3 somewhere in the code, it can optimize this into a common object. When you use the Number constructor, new memory is allocated for each instance.

+4
Dec 15 '08 at 19:43
source share

in JavaScript, the type of an object is not equal to another type of object, even if it has the same value, unless they are an EXACT POINT object.

In other words, in the Matthew example, n1 == n2 is false because you are comparing two REFERENCES with two SEPARATE objects, but n1 == n1 is true because you are comparing references to an EXACT SAME object.

So, although I now understand why using Number as a constructor can cause problems, I found that you can use the valueOf property when comparing Number objects.

In other words, n1.valueOf == n2.valueOf is true! (This is because you are comparing the return values ​​of valueOf FUNCTION, not REFERENCES with the objects themselves.)

This answer / summry was extracted from the question where it does not belong. Sub>

0
Jan 04 '17 at 2:47 on
source share



All Articles