如何在不使用具有误报的 isNaN 的情况下判断值是否为 NaN?
如何在不使用isNaN
函数的情况下检查输入值是否为NaN
?
How can I check whether the input value is NaN
or not without using the isNaN
function?
推荐答案
如果可以使用 ECMAScript 6,你有 Object.is
:
If you can use ECMAScript 6, you have Object.is
:
return Object.is(obj, NaN);
<小时>
否则,这里有一个选项,来自 underscore.js 的源代码:
// Is the given value `NaN`?
_.isNaN = function(obj) {
// `NaN` is the only value for which `===` is not reflexive.
return obj !== obj;
};
还有他们对该功能的说明:
Also their note for that function:
注意:这与原生 isNaN 函数不同,如果变量未定义,它也会返回 true.
Note: this is not the same as the native isNaN function, which will also return true if the variable is undefined.
相关文章