prefer-const

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

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

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

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

规则详情

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

k7HkpyhTRoT9T3bsrVJ1W9IUT56r3GvYicL0qS8XjwbDvDXG9eGk/CkswpMjh9k+

/*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);
}

f/g6SKVZdhoBiPVtnee0m+uPEv1T4kMgIaFUpxdtFwzAZHGBqJ5dJxRHxHHP/cP8

/*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(i);
}

// `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

Uv/kS1cOSk4ogTTbssJKCluf11gQJorZiMWONNuLPkRUEjcW9SArFc5y4/xfNIgN2QAppWk/IogvaLgm5KFM9g==

    zZVpP2woClVlwbE4HUgGIiNyga8+6nAmb+tOsxxfyOIvieupk9yp3CYjnEu90GeGPs+numB8jgtC5SK73xJG0wpWCXZt+eQlkT7VNLKjVsUJ2/kamWOlW48+FhWvWADNomlkhKK8roAgpn5vZS/ZtrCxhBGXX8em644PWEBnAzilepA4ZN9f1gGZkeQJBcljuWCYNKNu8d8buDxLGhnJwd7/KII3mvNGT105Krxxf+AiJ5pAr92feznilqmzvdpZT5b5GvHDnyXzPCUUETYouCXvUL9mj8WjLp2+Wlumls1zPG4VGDDixScIf++esVENtKvNgnrNY18ThW+LefFO4bA9TNEb1Ntu8LZOqKQKNy4NWYC6i0bhZI9+GXCccqZlqkzMBw4D/Cld3zwgfIbskw==

HXmalqewpOjDvakOhH2dIIKI5feF2TfIG2E8ajItIsoHcCrLrV0j64nkhH7GizIQ2w0exPXXsgOHufeeaHW722JHgUF1nFirBQvlC5CNIg8=

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

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

HXmalqewpOjDvakOhH2dIIKI5feF2TfIG2E8ajItIsoHcCrLrV0j64nkhH7GizIQYILOzFrqVGuSL+tCcpwHcK1/BRRy8KcGbklCHcy8mhY=

/*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;

xFp6TpOrl1QrZyo04emXdzQ+cT5o5ce0gsNm+sCcutSTLiTfqjPL6/enSbGkILgYg5S+15iujuzFz9SmW+IYBFDG9nqZUJ3M/kk+lFNVicQ=

/*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.*/

xFp6TpOrl1QrZyo04emXdzQ+cT5o5ce0gsNm+sCcutSFysCl0FVunDtDykiF2+dafaILQXnltdW6+fuxiRXO9TF8YYgmp7FN26UCaVIJxIU=

/*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

9HPdxOdMCIq0nkUh9v16QSqRxnt1VAlxmLD5Ho+is9TNV0hgw2QwpZE7mJnWHFrZhlCsQwvStW5j/ETneSqHDY9TimNNM/EWKW+DnmZGzWOOL4mCpKUJufhYkQjOf0M/IOTfvK8dkrYcXAZOnwyiefVH9duM0IcbdXx0ficcLBJRbHrqxBdUNqXGLju7rMDqrXIj81+JtZSOFUL0q3DoMPAPRUje4rHu2JmtnmIAPg5IqopGxIRUH0JOBDn7PERjF0Bz56le13gQLSwBkYPbzFSPGKjin6UvoiDpRf+imhNEzL7eDItUspkFjmDGBgjbILXHlwqwMGIoFEvt6X7e8LG2Mx+tWk92eTKOtJFaF5M=

BroOwuBuOdm6MkGWWs2Q6C7rl4NNZHCSm6/YRyutCqFJOVHrjjdIZfzngJEiGmSdCu3JDjxLkGsouCC1MB40Re+2BYTOKsUqpcHseFQjy1E=

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

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

O0e4CWdQpfe5OW80HO658UGl53K3NIszmCDnkCMrsqWwH+nVR0P+4xYxgFEI6Ak3jzButOXWrhKFkp1DmopRjNfW+MYVIojjHqlQNHK3VCNWIRtHRtGlNmkZAWEYlM5o

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

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

何时不使用

IPyb6IvnaMRpWJ+F26FWIFfsqS4SlxVJIqoxNcrSapbVL6eMb8OMmPnnn/q2NHahF5a4sHcRXZ9rrfyYn6B41oRreyv+utNGi/gk7N9A1gd0vu7m4yVy2EWba087fHl/iHQzo46osLz2sKUVCmgQjHOBW8pjzMUEWVTW/7u9NuU=