JavaScript中的isNaN和Number.isNaN之间的混淆 [英] Confusion between isNaN and Number.isNaN in javascript

查看:77
本文介绍了JavaScript中的isNaN和Number.isNaN之间的混淆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对NaN的工作方式感到困惑.我执行了isNaN(undefined),它返回了true.但是,如果我将使用Number.isNaN(undefined),它将返回false.所以我应该使用哪一个.也是为什么结果如此差异的原因.

I have a confusion in how NaN works. I have executed isNaN(undefined) it returned true . But if I will use Number.isNaN(undefined) it is returning false. So which one i should use. Also why there is so discrepancy in the result.

推荐答案

引用

Number.isNaN与ES5全局isNaN方法几乎相同. Number.isNaN返回提供的值是否等于NaN.这是一个 问题与这不是数字吗?"完全不同.

Number.isNaN is almost identical to ES5 global isNaN method. Number.isNaN returns whether the provided value equals NaN. This is a very different question from "is this not a number?".

因此,isNaN仅检查传递的值是否不是数字或不能转换为数字.另一方面,Number.isNaN仅检查该值是否等于NaN(尽管它使用的算法与===不同).

So isNaN just checks whether the passed value is not a number or cannot be converted into a Number. Number.isNaN on the other hand only checks if the value is equal to NaN (it uses a different algorithm than === though).

例如字符串'ponyfoo'不是数字,不能将其转换为数字,但是它不是NaN.

The String 'ponyfoo' for example is not a number and cannot be converted into a number, but it is not NaN.

示例:

Number.isNaN({});
// <- false, {} is not NaN
Number.isNaN('ponyfoo')
// <- false, 'ponyfoo' is not NaN
Number.isNaN(NaN)
// <- true, NaN is NaN
Number.isNaN('pony'/'foo')
// <- true, 'pony'/'foo' is NaN, NaN is NaN

isNaN({});
// <- true, {} is not a number
isNaN('ponyfoo')
// <- true, 'ponyfoo' is not a number
isNaN(NaN)
// <- true, NaN is not a number
isNaN('pony'/'foo')
// <- true, 'pony'/'foo' is NaN, NaN is not a number

这篇关于JavaScript中的isNaN和Number.isNaN之间的混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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