嗯,它们是semantically different,Number
constructor called as a function个表演type conversion和parseInt
个表演parsing,例如:
// parsing:
parseInt("20px"); // 20
parseInt("10100", 2); // 20
parseInt("2e1"); // 2
// type conversion
Number("20px"); // NaN
Number("2e1"); // 20, exponential notation
此外,parseInt
将忽略与当前使用的基数的任何数字不对应的尾随字符.
Number
构造函数不检测隐式八进制,但可以检测显式八进制记法:
Number("010"); // 10
Number("0o10") // 8, explicit octal
parseInt("010"); // 8, implicit octal
parseInt("010", 10); // 10, decimal radix used
它可以处理十六进制的数字,就像parseInt
:
Number("0xF"); // 15
parseInt("0xF"); //15
此外,执行数字类型转换的一个广泛使用的构造是Unary +
Operator (p. 72),它相当于使用Number
构造函数作为函数:
+"2e1"; // 20
+"0xF"; // 15
+"010"; // 10