How to tell whether value is NaN without using isNaN, which has false positives?(如何在不使用具有误报的 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.
这篇关于如何在不使用具有误报的 isNaN 的情况下判断值是否为 NaN?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在不使用具有误报的 isNaN 的情况下判断值是否为 NaN?


基础教程推荐
- Bokeh Div文本对齐 2022-01-01
- 在 contenteditable 中精确拖放 2022-01-01
- 如何添加到目前为止的天数? 2022-01-01
- 检查 HTML5 拖放文件类型 2022-01-01
- 即使用户允许,Gmail 也会隐藏外部电子邮件图片 2022-01-01
- npm start 错误与 create-react-app 2022-01-01
- fetch 是否支持原生多文件上传? 2022-01-01
- Bootstrap 模态出现在背景下 2022-01-01
- Fabric JS绘制具有活动形状的多边形 2022-01-01
- 原生拖动事件后如何获取 mouseup 事件? 2022-01-01