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?


- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- Flexslider 箭头未正确显示 2022-01-01
- 失败的 Canvas 360 jquery 插件 2022-01-01
- 400或500级别的HTTP响应 2022-01-01
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- Fetch API 如何获取响应体? 2022-01-01
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
- addEventListener 在 IE 11 中不起作用 2022-01-01