在线文档教程
Eslint
规则 | Rules

prefer-const

建议使用const(prefer-const)

在命令行上的--fix选项可以自动修复一些被这条规则反映的问题。

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

const 声明告诉读者,“这个变量永远不会被重新分配,”减少认知负荷并提高可维护性。

规则细节

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

此规则的错误代码示例:

/*eslint prefer-const: "error"*/ /*eslint-env es6*/ // it's initialized and never reassigned. let a = 3; console.log(a 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"*/ /*eslint-env es6*/ // 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 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 } // suggest to use `no-var` rule. var b = 3; console.log(b

选项

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

解构

在解构中解决变量的方式。有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 } }

何时不使用它

如果您不想收到有关初始分配后永远不会重新分配的变量的通知,则可以安全地禁用此规则。

相关规则

  • no-var

  • no-use-before-define

版本

该规则在 ESLint 0.23.0中引入。

资源