Confusion between isNaN and Number.isNaN in javascript(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.
推荐答案
引用一个 ponyfoo 关于 ES6 中数字的文章:
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).
例如String '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之间的混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:javascript中isNaN和Number.isNaN之间的混淆
基础教程推荐
- html表格如何通过更改悬停边框来突出显示列? 2022-01-01
- 如何使用JIT在顺风css中使用布局变体? 2022-01-01
- 直接将值设置为滑块 2022-01-01
- Vue 3 – <过渡>渲染不能动画的非元素根节点 2022-01-01
- 我可以在浏览器中与Babel一起使用ES模块,而不捆绑我的代码吗? 2022-01-01
- 如何使用TypeScrip将固定承诺数组中的项设置为可选 2022-01-01
- Chart.js 在线性图表上拖动点 2022-01-01
- Electron 将 Node.js 和 Chromium 上下文结合起来意味着 2022-01-01
- 用于 Twitter 小部件宽度的 HTML/CSS 2022-01-01
- 自定义 XMLHttpRequest.prototype.open 2022-01-01