eqeqeq
要求使用 === 和 !==
使用类型安全的相等运算符 === 和 !== 而不是它们的常规对应项 == 和 != 被认为是一种很好的做法。
¥It is considered good practice to use the type-safe equality operators === and !== instead of their regular counterparts == and !=.
原因是 == 和 != 在相当晦涩的 抽象等式比较算法 之后进行类型强制。例如,以下语句都被视为 true:
¥The reason for this is that == and != do type coercion which follows the rather obscure Abstract Equality Comparison Algorithm.
For instance, the following statements are all considered true:
-
[] == false -
[] == ![] -
3 == "03"
如果其中一个出现在诸如 a == b 之类的看似无辜的声明中,则很难发现实际问题。
¥If one of those occurs in an innocent-looking statement such as a == b the actual problem is very difficult to spot.
规则详情
¥Rule Details
此规则旨在消除类型不安全的相等运算符。
¥This rule is aimed at eliminating the type-unsafe equality operators.
此规则的错误代码示例:
¥Examples of incorrect code for this rule:
/*eslint eqeqeq: "error"*/
if (x == 42) { }
if ("" == text) { }
if (obj.getStuff() != undefined) { }
命令行中的 --fix 选项会自动修复此规则报告的一些问题。只有当其中一个操作数是 typeof 表达式,或者两个操作数都是具有相同类型的字面时,才会解决问题。
¥The --fix option on the command line automatically fixes some problems reported by this rule. A problem is only fixed if one of the operands is a typeof expression, or if both operands are literals with the same type.
选项
¥Options
always
"always" 选项(默认)强制在每种情况下使用 === 和 !==(除非你选择更具体地处理 null [见下文])。
¥The "always" option (default) enforces the use of === and !== in every situation (except when you opt-in to more specific handling of null [see below]).
"always" 选项的错误代码示例:
¥Examples of incorrect code for the "always" option:
/*eslint eqeqeq: ["error", "always"]*/
a == b
foo == true
bananas != 1
value == undefined
typeof foo == 'undefined'
'hello' != 'world'
0 == 0
true == true
foo == null
"always" 选项的正确代码示例:
¥Examples of correct code for the "always" option:
/*eslint eqeqeq: ["error", "always"]*/
a === b
foo === true
bananas !== 1
value === undefined
typeof foo === 'undefined'
'hello' !== 'world'
0 === 0
true === true
foo === null
此规则可选地采用第二个参数,它应该是具有以下受支持属性的对象:
¥This rule optionally takes a second argument, which should be an object with the following supported properties:
-
"null":自定义此规则如何处理null字面。可能的值:¥
"null": Customize how this rule treatsnullliterals. Possible values:-
always(默认) - 始终使用===或!==。¥
always(default) - Always use===or!==. -
never- 切勿将===或!==与null一起使用。¥
never- Never use===or!==withnull. -
ignore- 不要将此规则应用于null。¥
ignore- Do not apply this rule tonull.
-
smart
"smart" 选项强制使用 === 和 !==,但以下情况除外:
¥The "smart" option enforces the use of === and !== except for these cases:
-
比较两个字面量值。
¥Comparing two literal values.
-
评估
typeof的值。¥Evaluating the value of
typeof. -
与
null进行比较。¥Comparing against
null.
"smart" 选项的错误代码示例:
¥Examples of incorrect code for the "smart" option:
/*eslint eqeqeq: ["error", "smart"]*/
// comparing two variables requires ===
a == b
// only one side is a literal
foo == true
bananas != 1
// comparing to undefined requires ===
value == undefined
"smart" 选项的正确代码示例:
¥Examples of correct code for the "smart" option:
/*eslint eqeqeq: ["error", "smart"]*/
typeof foo == 'undefined'
'hello' != 'world'
0 == 0
true == true
foo == null
allow-null
已弃用:不要使用此选项,而是使用 "always" 并传递值为 "ignore" 的 "null" 选项属性。这将告诉 ESLint 始终强制执行严格相等,除非与 null 字面进行比较。
¥Deprecated: Instead of using this option use "always" and pass a "null" option property with value "ignore". This will tell ESLint to always enforce strict equality except when comparing with the null literal.
["error", "always", {"null": "ignore"}]
何时不使用
¥When Not To Use It
如果你不想强制使用相等运算符的样式,那么禁用此规则是安全的。
¥If you don’t want to enforce a style for using equality operators, then it’s safe to disable this rule.
版本
此规则是在 ESLint v0.0.2 中引入。