版本

prefer-const

要求對於宣告後永遠不會重新賦值的變數使用 const 宣告

🔧 可修正

此規則報告的一些問題可以使用 --fix 命令列選項自動修正

如果變數永遠不會重新賦值,使用 const 宣告會更好。

const 宣告會告訴讀者:「這個變數永遠不會重新賦值」,減少認知負荷並提高可維護性。

規則詳細資訊

此規則旨在標記使用 let 關鍵字宣告,但在初始賦值後從未重新賦值的變數。

此規則的錯誤程式碼範例

在遊樂場開啟
/*eslint prefer-const: "error"*/

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

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

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 b;
console.log(b);

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

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

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

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

// `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();

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

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

選項

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

解構

在解構中處理變數的方式種類。有 2 個值

  • "any" (預設) - 如果解構中的任何變數應該是 const,此規則會警告這些變數。
  • "all" - 如果解構中的所有變數都應該是 const,此規則會警告這些變數。否則,將忽略它們。

預設 {"destructuring": "any"} 選項的錯誤程式碼範例

在遊樂場開啟
/*eslint prefer-const: "error"*/

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

預設 {"destructuring": "any"} 選項的正確程式碼範例

在遊樂場開啟
/*eslint prefer-const: "error"*/

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

// all variables are reassigned.
let {c, d} = obj;
c = c + 1;
d = d + 1;

{"destructuring": "all"} 選項的錯誤程式碼範例

在遊樂場開啟
/*eslint prefer-const: ["error", {"destructuring": "all"}]*/

// 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"}]*/

// '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}]*/

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

預設 {"ignoreReadBeforeAssign": false} 選項的正確程式碼範例

在遊樂場開啟
/*eslint prefer-const: ["error", {"ignoreReadBeforeAssign": false}]*/

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

何時不該使用它

如果您不希望收到關於初始賦值後從未重新賦值的變數的通知,您可以安全地停用此規則。

版本

此規則在 ESLint v0.23.0 中引入。

資源

變更語言