我正在转换一些现有的代码来遵循ECMA脚本,并且我使用ESLint来遵循编码标准。我有以下ecmascript方法
static getArrayOfIndices(text, char) {
let resultArray = [];
let index = text.indexOf(char);
const lastIndex = text.lastIndexOf(char);
while (index <= lastIndex && index !== -1) {
resultArray.push(index);
if (index < lastIndex) {
index = text.substr(index + 1).indexOf(char) + index + 1;
} else {
index = lastIndex + 1999; // some random addition to fail test condition on next iteration
}
}
return resultArray;
}
对于resultArray的声明,ESLint抛出错误
ESLint: `resultArray` is never modified, use `const`instead. (prefer-const)
但是由于元素被推入数组中,它不是被修改了吗?
发布于 2016-01-14 05:09:27
要理解此错误,必须了解const
声明的变量持有对值的只读引用。但这并不意味着它所持有的值是不可变的[mdn条款]。
由于您只是在更改变量的成员,而不是对绑定执行重新分配,es-lint的prefer-const
规则警告您,可以使用const
声明的变量而不是let
声明的变量。
https://stackoverflow.com/questions/34784588
复制相似问题