永远不会重新分配“数字".使用"const"代替. (首选const) [英] 'number' is never reassigned. Use 'const' instead. (prefer-const)

查看:352
本文介绍了永远不会重新分配“数字".使用"const"代替. (首选const)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么在这种情况下eslint 4.17.0我从来没有重新分配错误号.使用"const"代替. (首选const).为什么我需要使用const?请给我解释我听不懂.

Why in this case eslint 4.17.0 i have error number is never reassigned. Use 'const' instead. (prefer-const). Why i need to use const? Please, explain me i can't understand.

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

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

ecmascript

ecmascript

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

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

夹板问题

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

推荐答案

ES6常量并不表示值是常量"或不可变的.一种 const值肯定可以更改.以下是完全有效的 不引发异常的ES6代码.

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.

In your case, if you know that you are gonna change the properties, try using let.

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

这篇关于永远不会重新分配“数字".使用"const"代替. (首选const)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆