如何在不使用具有误报的 isNaN 的情况下判断值是否为 NaN? [英] How to tell whether value is NaN without using isNaN, which has false positives?

查看:19
本文介绍了如何在不使用具有误报的 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆