gpt4 book ai didi

javascript - 'number' 永远不会重新分配。请改用 'const'。 (首选常量)

转载 作者:行者123 更新时间:2023-11-29 11:00:04 24 4
gpt4 key购买 nike

为什么在这种情况下 eslint 4.17.0 i have error number 永远不会重新分配。请改用“常量”。 (首选常量)。为什么我需要使用常量?请解释一下,我无法理解。

let test = {
'number': 1,
'string': 'asd',
};
test.number = 99;

console.log(test.number);
// output: 99

脚本

 {
"parser": "babel-eslint",
"env": {
"browser": true
},
"extends": [
"google"
],
"rules": {
"prefer-const": 2

},
"parserOptions": {
"ecmaVersion": 6,
"sourceType": "module"
}
}

eslint问题

[eslint] 'test' is never reassigned. Use 'const' insted. (prefer-const)

最佳答案

ES6 const does not indicate that a value is ‘constant’ or immutable. A const value can definitely change. The following is perfectly valid ES6 code that does not throw an exception.

const foo = {};
foo.bar = 42;
console.log(foo.bar);
// → 42

在您的情况下,如果您知道要更改属性,请尝试使用 let。

看这里:https://mathiasbynens.be/notes/es6-const

关于javascript - 'number' 永远不会重新分配。请改用 'const'。 (首选常量),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48707353/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com