版本

no-multi-assign

禁止使用链式赋值表达式

变量赋值链可能导致意外结果,并且难以阅读。

(function() {
    const foo = bar = 0; // Did you mean `foo = bar == 0`?
    bar = 1;             // This will not fail since `bar` is not constant.
})();
console.log(bar);        // This will output 1 since `bar` is not scoped.

规则详情

此规则禁止在一个语句中使用多个赋值。

此规则的**错误**代码示例

在代码游乐场中打开
/*eslint no-multi-assign: "error"*/

var a = b = c = 5;

const foo = bar = "baz";

let d =
    e =
    f;

class Foo {
    a = b = 10;
}

a = b = "quux";

此规则的**正确**代码示例

在代码游乐场中打开
/*eslint no-multi-assign: "error"*/

var a = 5;
var b = 5;
var c = 5;

const foo = "baz";
const bar = "baz";

let d = c;
let e = c;

class Foo {
    a = 10;
    b = 10;
}

a = "quux";
b = "quux";

选项

此规则有一个对象选项

  • "ignoreNonDeclaration": 当设置为true时,该规则允许不包含在声明中初始化变量或初始化类字段的链。默认值为false

ignoreNonDeclaration

{ "ignoreNonDeclaration": true }选项的**正确**代码示例

在代码游乐场中打开
/*eslint no-multi-assign: ["error", { "ignoreNonDeclaration": true }]*/

let a;
let b;
a = b = "baz";

const x = {};
const y = {};
x.one = y.one = 1;

{ "ignoreNonDeclaration": true }选项的**错误**代码示例

在代码游乐场中打开
/*eslint no-multi-assign: ["error", { "ignoreNonDeclaration": true }]*/

let a = b = "baz";

const foo = bar = 1;

class Foo {
    a = b = 10;
}

版本

此规则在 ESLint v3.14.0 中引入。

资源

更改语言