Created
November 21, 2010 15:33
-
-
Save founddrama/708820 to your computer and use it in GitHub Desktop.
why you should basically never use the Number constructor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var n = new Number(x); // where x is a global storing an incrementer | |
function doMath(a){ | |
if (typeof a == "number") { | |
return a + x; | |
} | |
} | |
doMath(n); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var n = new Number(1); | |
alert(n); // 1 | |
typeof n; // object | |
n instanceof Number; // true | |
var nn = 1; | |
alert(nn); // 1 | |
typeof nn; // number | |
n instanceof Number; // false | |
n == nn; // true | |
typeof n == typeof nn; // false | |
n++; | |
nn++; | |
alert(n); // 2 | |
alert(nn); // 2 | |
n == nn; // true | |
typeof n; // number | |
typeof nn; // number | |
typeof n == typeof nn; // true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@see
http://blog.founddrama.net/2010/07/why-you-should-basically-never-use-the-number-constructor/