试一试:在Node.js或Firefox中打开REPL,并键入:
Number.isNaN('asdf');
这怎么可能不是
NaN
呢?如果不是NaN
,那它又是什么呢?试一试:在Node.js或Firefox中打开REPL,并键入:
Number.isNaN('asdf');
NaN
呢?如果不是NaN
,那它又是什么呢?请注意,Number.IsNaN
的支持受到限制。
输出结果如下:
console.log(Number.isNaN("blabla"));
给出false
和false !== NaN
:
console.log(false !== NaN)
console.log(false !== Number.NaN);
这里有一个参考资料:
When the Number.isNaN is called with one argument number, the following steps are taken:
If Type(number) is not Number, return false. If number is NaN, return true. Otherwise, return false.
This function differs from the global isNaN function (18.2.3) is that it does not convert its argument to a Number before determining whether it is NaN.
我很厉害,因为我有1337声望。
Number.isNaN()
的概念。唯一指定的函数是全局函数isNaN
(15.1.2.4),它将正确地返回true
,对于任何不是数字或不能正确转换为数字的内容。
Number.isNaN
(返回true
的值为NaN
)可能是下一个标准草案的一部分(请参见remyabel的答案),它可以被isNaN
内部使用。这是ECMAScript 6标准的一部分。
Number.isNaN
返回一个值是否为NaN,而不会强制转换参数。
Number.isNaN("xmfds"); // false
Number.isNaN("ose"); // false
isNaN
返回一个布尔值,指示在将参数强制转换为 Number
类型后,该值是否为 NaN。
isNaN("ose") -> isNaN( Number("ose") ) -> isNaN(NaN); // true
试试这个:
function checkNaN(value)
{
return value !== value;
}
它能够工作是因为(我相信)NaN 是 JavaScript 中唯一不严格等于其自身的东西。
Number.isNaN(Number('asdf'))
- zamnuts