# prefer-const

声明后从不重新分配的变量需要 const 声明

一些该规则报告的问题可以通过 --fix 命令行选项 自动修复

如果从不重新分配变量,则使用 const 声明会更好。

const 宣言告诉读者,"this variable is never reassigned," 减少认知负荷,提高可维护性。

# 规则详情

此规则旨在标记使用 let 关键字声明但在初始分配后从未重新分配的变量。

此规则的错误代码示例:

/*eslint prefer-const: "error"*/

// it's initialized and never reassigned.
let a = 3;
console.log(a);

let a;
a = 0;
console.log(a);

class C {
    static {
        let a;
        a = 0;
        console.log(a);
    }
}

// `i` is redefined (not reassigned) on each loop step.
for (let i in [1, 2, 3]) {
    console.log(i);
}

// `a` is redefined (not reassigned) on each loop step.
for (let a of [1, 2, 3]) {
    console.log(a);
}

此规则的正确代码示例:

/*eslint prefer-const: "error"*/

// using const.
const a = 0;

// it's never initialized.
let a;
console.log(a);

// it's reassigned after initialized.
let a;
a = 0;
a = 1;
console.log(a);

// it's initialized in a different block from the declaration.
let a;
if (true) {
    a = 0;
}
console.log(a);

// it's initialized in a different scope.
let a;
class C {
    #x;
    static {
        a = obj => obj.#x;
    }
}

// it's initialized at a place that we cannot write a variable declaration.
let a;
if (true) a = 0;
console.log(a);

// `i` gets a new binding each iteration
for (const i in [1, 2, 3]) {
  console.log(i);
}

// `a` gets a new binding each iteration
for (const a of [1, 2, 3]) {
  console.log(a);
}

// `end` is never reassigned, but we cannot separate the declarations without modifying the scope.
for (let i = 0, end = 10; i < end; ++i) {
    console.log(a);
}

// `predicate` is only assigned once but cannot be separately declared as `const`
let predicate;
[object.type, predicate] = foo();

// `a` is only assigned once but cannot be separately declared as `const`
let a;
const b = {};
({ a, c: b.c } = func());

// suggest to use `no-var` rule.
var b = 3;
console.log(b);

# 选项

{
    "prefer-const": ["error", {
        "destructuring": "any",
        "ignoreReadBeforeAssign": false
    }]
}

# destructuring

在解构中处理变量的那种方式。有2个值:

  • "any"(默认)- 如果解构中的任何变量应该是 const,则此规则会针对这些变量发出警告。
  • "all" - 如果解构中的所有变量都应为 const,则此规则会警告变量。否则,忽略它们。

默认 {"destructuring": "any"} 选项的错误代码示例:

/*eslint prefer-const: "error"*/
/*eslint-env es6*/

let {a, b} = obj;    /*error 'b' is never reassigned, use 'const' instead.*/
a = a + 1;

默认 {"destructuring": "any"} 选项的正确代码示例:

/*eslint prefer-const: "error"*/
/*eslint-env es6*/

// using const.
const {a: a0, b} = obj;
const a = a0 + 1;

// all variables are reassigned.
let {a, b} = obj;
a = a + 1;
b = b + 1;

{"destructuring": "all"} 选项的错误代码示例:

/*eslint prefer-const: ["error", {"destructuring": "all"}]*/
/*eslint-env es6*/

// all of `a` and `b` should be const, so those are warned.
let {a, b} = obj;    /*error 'a' is never reassigned, use 'const' instead.
                             'b' is never reassigned, use 'const' instead.*/

{"destructuring": "all"} 选项的正确代码示例:

/*eslint prefer-const: ["error", {"destructuring": "all"}]*/
/*eslint-env es6*/

// 'b' is never reassigned, but all of `a` and `b` should not be const, so those are ignored.
let {a, b} = obj;
a = a + 1;

# ignoreReadBeforeAssign

这是一个避免与 no-use-before-define 规则冲突的选项(没有 "nofunc" 选项)。如果指定了 true,此规则将忽略在声明和第一次赋值之间读取的变量。默认为 false

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

/*eslint prefer-const: ["error", {"ignoreReadBeforeAssign": true}]*/
/*eslint-env es6*/

let timer;
function initialize() {
    if (foo()) {
        clearInterval(timer);
    }
}
timer = setInterval(initialize, 100);

默认 {"ignoreReadBeforeAssign": false} 选项的正确代码示例:

/*eslint prefer-const: ["error", {"ignoreReadBeforeAssign": false}]*/
/*eslint-env es6*/

const timer = setInterval(initialize, 100);
function initialize() {
    if (foo()) {
        clearInterval(timer);
    }
}

# 何时不使用

如果您不希望收到有关在初始分配后从未重新分配的变量的通知,您可以安全地禁用此规则。

Last Updated: 5/13/2023, 8:55:38 PM