首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

no-const-assign

配置文件中的"extends": "eslint:recommended"属性启用此规则。

我们不能修改使用const关键字声明的变量。它会引发运行时错误。

在非 ES2015环境下,它可能仅仅被忽略。

规则细节

此规则旨在标记修改使用const关键字声明的变量。

此规则的错误代码示例:

代码语言:javascript
复制
/*eslint no-const-assign: "error"*/
/*eslint-env es6*/

const a = 0;
a = 1;
代码语言:javascript
复制
/*eslint no-const-assign: "error"*/
/*eslint-env es6*/

const a = 0;
a += 1;
代码语言:javascript
复制
/*eslint no-const-assign: "error"*/
/*eslint-env es6*/

const a = 0;
++a;

此规则的正确代码示例:

代码语言:javascript
复制
/*eslint no-const-assign: "error"*/
/*eslint-env es6*/

const a = 0;
console.log(a);
代码语言:javascript
复制
/*eslint no-const-assign: "error"*/
/*eslint-env es6*/

for (const a in [1, 2, 3]) { // `a` is re-defined (not modified) on each loop step.
    console.log(a);
}
代码语言:javascript
复制
/*eslint no-const-assign: "error"*/
/*eslint-env es6*/

for (const a of [1, 2, 3]) { // `a` is re-defined (not modified) on each loop step.
    console.log(a);
}

何时不使用它

如果您不希望收到有关修改使用const关键字声明的变量的通知,则可以安全地禁用此规则。

版本

该规则在 ESLint 1.0.0-rc-1中引入。

资源

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com