no-ternary
Disallow ternary operators
三元运算符用于有条件地给一个变量赋值。有些人认为,使用三元运算符会导致代码不清晰。
var foo = isBar ? baz : qux;
规则细节
这条规则不允许三元运算符。
使用此规则的错误示例:
Open in Playground
/*eslint no-ternary: "error"*/
var foo = isBar ? baz : qux;
function quux() {
return foo ? bar() : baz();
}
使用此规则的正确示例:
Open in Playground
/*eslint no-ternary: "error"*/
var foo;
if (isBar) {
foo = baz;
} else {
foo = qux;
}
function quux() {
if (foo) {
return bar();
} else {
return baz();
}
}
Related Rules
Version
This rule was introduced in ESLint v0.0.9.