use-isnan
NOTE: this rule is part of the
推荐
rule set.Enable full set in
deno.json
:{ "lint": { "rules": { "tags": ["推荐"] } } }
Enable full set using the Deno CLI:
deno lint --rules-tags=推荐
This rule can be explictly included to or excluded from the rules present in the current tag by adding it to the
include
or exclude
array in deno.json
:{ "lint": { "rules": { "include": ["use-isnan"], "exclude": ["use-isnan"] } } }
不允许与 NaN
进行比较。
因为 NaN
在 JavaScript 中是独特的,它不等于任何东西,包括它自己,所以与 NaN
的比较结果是令人困惑的:
NaN === NaN
或NaN == NaN
的结果为false
NaN !== NaN
或NaN != NaN
的结果为true
因此,这条规则要求使用 isNaN()
或 Number.isNaN()
来判断值是否为 NaN
。
无效示例:
if (foo == NaN) {
// ...
}
if (foo != NaN) {
// ...
}
switch (NaN) {
case foo:
// ...
}
switch (foo) {
case NaN:
// ...
}
有效示例:
if (isNaN(foo)) {
// ...
}
if (!isNaN(foo)) {
// ...
}