useIsNan
诊断类别:lint/correctness/useIsNan
¥Diagnostic Category: lint/correctness/useIsNan
自从:v1.0.0
¥Since: v1.0.0
来源:
¥Sources:
要求在检查 NaN 时调用 isNaN()。
¥Require calls to isNaN() when checking for NaN.
在 JavaScript 中,NaN 是 Number 类型的特殊值。它用于表示由 IEEE 二进制浮点算术标准指定的双精度 64 位格式表示的任何 “not-a-number” 值。
¥In JavaScript, NaN is a special value of the Number type.
It’s used to represent any of the “not-a-number” values represented by the double-precision 64-bit format as specified by the IEEE Standard for Binary Floating-Point Arithmetic.
因为 NaN 在 JavaScript 中是唯一的,不等于任何东西,包括它自己,所以与 NaN 的比较结果令人困惑:
¥Because NaN is unique in JavaScript by not being equal to anything, including itself, the results of comparisons to NaN are confusing:
-
NaN===NaN或NaN==NaN计算结果为 false¥
NaN===NaNorNaN==NaNevaluate to false -
NaN!==NaN或NaN!=NaN计算结果为 true¥
NaN!==NaNorNaN!=NaNevaluate to true
因此,使用 Number.isNaN() 或全局 isNaN() 函数来测试值是否为 NaN。
¥Therefore, use Number.isNaN() or global isNaN() functions to test whether a value is NaN.
请注意 Number.isNaN() 和 isNaN() 没有相同的行为。当 isNaN() 的参数不是数字时,首先将值强制转换为数字。Number.isNaN() 不执行此强制。因此,这是一种更可靠的方法来测试值是否为 NaN。
¥Note that Number.isNaN() and isNaN() do not have the same behavior.
When the argument to isNaN() is not a number, the value is first coerced to a number.
Number.isNaN() does not perform this coercion.
Therefore, it is a more reliable way to test whether a value is NaN.
¥Examples
¥Invalid
123 == NaNcode-block.js:1:1 lint/correctness/useIsNan FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Use the Number.isNaN function to compare with NaN.
> 1 │ 123 == NaN
│ ^^^^^^^^^^
2 │
ℹ Unsafe fix: Use Number.isNaN() instead.
1 │ - 123·==·NaN
1 │ + Number.isNaN(123)
2 2 │
123 != NaNcode-block.js:1:1 lint/correctness/useIsNan FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Use the Number.isNaN function to compare with NaN.
> 1 │ 123 != NaN
│ ^^^^^^^^^^
2 │
ℹ Unsafe fix: Use Number.isNaN() instead.
1 │ - 123·!=·NaN
1 │ + !Number.isNaN(123)
2 2 │
switch(foo) { case (NaN): break; }code-block.js:1:20 lint/correctness/useIsNan ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ ‘case NaN’ can never match. Use Number.isNaN before the switch.
> 1 │ switch(foo) { case (NaN): break; }
│ ^^^^^
2 │
Number.NaN == "abc"code-block.js:1:1 lint/correctness/useIsNan FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Use the Number.isNaN function to compare with NaN.
> 1 │ Number.NaN == “abc”
│ ^^^^^^^^^^^^^^^^^^^
2 │
ℹ Unsafe fix: Use Number.isNaN() instead.
1 │ - Number.NaN·==·“abc”
1 │ + Number.isNaN(“abc”)
2 2 │
¥Valid
if (Number.isNaN(123) !== true) {}
foo(Number.NaN / 2)
switch(foo) {}¥Related links