use-isnan
检查 NaN
时需要调用 isNaN()
在 配置文件 中使用来自 @eslint/js
的 recommended
配置可以启用此规则
此规则报告的一些问题可通过编辑器建议手动修复
在 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
评估为假¥
NaN === NaN
orNaN == NaN
evaluate to false -
NaN !== NaN
或NaN != NaN
评估为真¥
NaN !== NaN
orNaN != NaN
evaluate to true
因此,使用 Number.isNaN()
或全局 isNaN()
函数来测试一个值是否为 NaN
。
¥Therefore, use Number.isNaN()
or global isNaN()
functions to test whether a value is NaN
.
规则详情
¥Rule Details
此规则不允许与 ‘NaN’ 进行比较。
¥This rule disallows comparisons to ‘NaN’.
此规则的错误代码示例:
¥Examples of incorrect code for this rule:
/*eslint use-isnan: "error"*/
if (foo == NaN) {
// ...
}
if (foo != NaN) {
// ...
}
if (foo == Number.NaN) {
// ...
}
if (foo != Number.NaN) {
// ...
}
此规则的正确代码示例:
¥Examples of correct code for this rule:
/*eslint use-isnan: "error"*/
if (isNaN(foo)) {
// ...
}
if (!isNaN(foo)) {
// ...
}
选项
¥Options
此规则有一个对象选项,有两个选项:
¥This rule has an object option, with two options:
-
"enforceForSwitchCase": true
(默认)另外不允许在switch
语句中使用case NaN
和switch(NaN)
。¥
"enforceForSwitchCase": true
(default) additionally disallowscase NaN
andswitch(NaN)
inswitch
statements. -
"enforceForIndexOf": true
另外不允许将indexOf
和lastIndexOf
方法与NaN
一起使用。默认为false
,这意味着默认情况下此规则不会警告indexOf(NaN)
或lastIndexOf(NaN)
方法调用。¥
"enforceForIndexOf": true
additionally disallows the use ofindexOf
andlastIndexOf
methods withNaN
. Default isfalse
, meaning that this rule by default does not warn aboutindexOf(NaN)
orlastIndexOf(NaN)
method calls.
enforceForSwitchCase
switch
语句在内部使用 ===
比较来将表达式的值与 case 子句相匹配。因此,它永远无法匹配 case NaN
。此外,switch(NaN)
永远不能匹配 case 子句。
¥The switch
statement internally uses the ===
comparison to match the expression’s value to a case clause.
Therefore, it can never match case NaN
. Also, switch(NaN)
can never match a case clause.
此规则的错误代码示例("enforceForSwitchCase"
选项设置为 true
(默认)):
¥Examples of incorrect code for this rule with "enforceForSwitchCase"
option set to true
(default):
/*eslint use-isnan: ["error", {"enforceForSwitchCase": true}]*/
switch (foo) {
case NaN:
bar();
break;
case 1:
baz();
break;
// ...
}
switch (NaN) {
case a:
bar();
break;
case b:
baz();
break;
// ...
}
switch (foo) {
case Number.NaN:
bar();
break;
case 1:
baz();
break;
// ...
}
switch (Number.NaN) {
case a:
bar();
break;
case b:
baz();
break;
// ...
}
此规则的正确代码示例,其中 "enforceForSwitchCase"
选项设置为 true
(默认):
¥Examples of correct code for this rule with "enforceForSwitchCase"
option set to true
(default):
/*eslint use-isnan: ["error", {"enforceForSwitchCase": true}]*/
if (Number.isNaN(foo)) {
bar();
} else {
switch (foo) {
case 1:
baz();
break;
// ...
}
}
if (Number.isNaN(a)) {
bar();
} else if (Number.isNaN(b)) {
baz();
} // ...
此规则的正确代码示例("enforceForSwitchCase"
选项设置为 false
):
¥Examples of correct code for this rule with "enforceForSwitchCase"
option set to false
:
/*eslint use-isnan: ["error", {"enforceForSwitchCase": false}]*/
switch (foo) {
case NaN:
bar();
break;
case 1:
baz();
break;
// ...
}
switch (NaN) {
case a:
bar();
break;
case b:
baz();
break;
// ...
}
switch (foo) {
case Number.NaN:
bar();
break;
case 1:
baz();
break;
// ...
}
switch (Number.NaN) {
case a:
bar();
break;
case b:
baz();
break;
// ...
}
enforceForIndexOf
以下方法在内部使用 ===
比较来将给定值与数组元素匹配:
¥The following methods internally use the ===
comparison to match the given value with an array element:
因此,对于任何数组 foo
,foo.indexOf(NaN)
和 foo.lastIndexOf(NaN)
将始终返回 -1
。
¥Therefore, for any array foo
, foo.indexOf(NaN)
and foo.lastIndexOf(NaN)
will always return -1
.
如果你希望此规则报告 indexOf(NaN)
和 lastIndexOf(NaN)
方法调用,请将 "enforceForIndexOf"
设置为 true
。
¥Set "enforceForIndexOf"
to true
if you want this rule to report indexOf(NaN)
and lastIndexOf(NaN)
method calls.
此规则的错误代码示例("enforceForIndexOf"
选项设置为 true
):
¥Examples of incorrect code for this rule with "enforceForIndexOf"
option set to true
:
/*eslint use-isnan: ["error", {"enforceForIndexOf": true}]*/
var hasNaN = myArray.indexOf(NaN) >= 0;
var firstIndex = myArray.indexOf(NaN);
var lastIndex = myArray.lastIndexOf(NaN);
var indexWithSequenceExpression = myArray.indexOf((doStuff(), NaN));
var firstIndexFromSecondElement = myArray.indexOf(NaN, 1);
var lastIndexFromSecondElement = myArray.lastIndexOf(NaN, 1);
此规则的正确代码示例("enforceForIndexOf"
选项设置为 true
):
¥Examples of correct code for this rule with "enforceForIndexOf"
option set to true
:
/*eslint use-isnan: ["error", {"enforceForIndexOf": true}]*/
function myIsNaN(val) {
return typeof val === "number" && isNaN(val);
}
function indexOfNaN(arr) {
for (var i = 0; i < arr.length; i++) {
if (myIsNaN(arr[i])) {
return i;
}
}
return -1;
}
function lastIndexOfNaN(arr) {
for (var i = arr.length - 1; i >= 0; i--) {
if (myIsNaN(arr[i])) {
return i;
}
}
return -1;
}
var hasNaN = myArray.some(myIsNaN);
var hasNaN = indexOfNaN(myArray) >= 0;
var firstIndex = indexOfNaN(myArray);
var lastIndex = lastIndexOfNaN(myArray);
// ES2015
var hasNaN = myArray.some(Number.isNaN);
// ES2015
var firstIndex = myArray.findIndex(Number.isNaN);
// ES2016
var hasNaN = myArray.includes(NaN);
已知限制
¥Known Limitations
此选项检查具有给定名称的方法,即使具有该方法的对象不是数组。
¥This option checks methods with the given names, even if the object which has the method is not an array.
版本
此规则是在 ESLint v0.0.6 中引入。