no-useless-computed-key

禁止对象和类中不必要的计算属性键

一些该规则报告的问题可以通过 --fix 命令行选项 自动修复

没有必要将计算属性与字面一起使用,例如:

var foo = {["a"]: "b"};

代码可以重写为:

var foo = {"a": "b"};

规则详情

此规则不允许不必要地使用计算的属性键。

GwS4EaSovppQ4mqJLqfbKzSYv3y9ds7cGwzaMFa/7z4C1gY8WlNlp/RO4mqG79th

/*eslint no-useless-computed-key: "error"*/

var a = { ['0']: 0 };
var a = { ['0+1,234']: 0 };
var a = { [0]: 0 };
var a = { ['x']: 0 };
var a = { ['x']() {} };

tvK23x9kXaq858zZ8KSDe/grTZY5yefhuA6McIGYa61ulXJimhCmlRPtj/MyWaVD

/*eslint no-useless-computed-key: "error"*/

var c = { 'a': 0 };
var c = { 0: 0 };
var a = { x() {} };
var c = { a: 0 };
var c = { '0+1,234': 0 };

Ttf8akTnfMaXpYU9MQ39atGRqhiJoQbp5E2LmQBF0OEhbed4wh/d3SMiQLwaZR+i

/*eslint no-useless-computed-key: "error"*/

var c = {
    "__proto__": foo, // defines object's prototype

    ["__proto__"]: bar // defines a property named "__proto__"
};

选项

lagSbevXYrfbXbjPe46SgS5AeQXYUI2nHp1Oi4G2rpupLUMFcQkF4qiedCaXpNGQ

    ghSFfrzmktyYBrVeTYzcnyZH1QjDQwl+QHX142zJlo23AS70BFxl0G/WJfNf37KBBq0P09wypMpLG+ePYffd2v5HcdF25g7TK/YZK9PNlWefQtIFuumW0GypYWitpacXV4WGH/RnEY0NhCCKzeB5emFmYoHJFCyyzQ5oI/8KlBcpJy78KrQVY58/bTVPVczGZnTRvLNWFiQy0FYk8Q53Ng==

enforceForClassMembers

aDc64ZzIN2OX4/JVa1N2E0YZFsB6Vhqj99Lu3wHEzjzZmxG012qA/2n86u25xpMmnY/l16pStbO3Kl63PQfHkKY63RS0AYIyJVVkfyS8RUhHrPp3dO7+s+5x1BFvwdTkXzvKBNbV706DFDNC6kFcdBM0uBuUSPSfs3z6jcOwiV4+Vza4ownspbwgBWb8reHU

s7IZMop6EZzbNsW7jSJgKA/lU+Jnn6wRokiSYC8se+sAS38sSdkmrK/Me98lXES1JL3UkaDI9m1XRnKp/lnKd9v0pqAl86kqt1BtOpKQXB6ND11IYlY7OhtU6eRNVwtaZ5fnnfgqiGY6KlHqzbt5NUwhLCCGdiLcf79vcba1d/WzCNt35Fg8yjtXO1NHITt/Bcf5IsM+pv4flx3V8FeTVN8l+5M6ySVCX/9XvldvNtLpNf5ZmM0/aAHL7DYvAiuA

qVCMgjR5tKUNSBmRY5fDOf+I8msP/Eu4kA2GpuRUuMyrrB6hISlehS5gFUNCadklfNyIJdJp2ixhIGRO+KUcfuldE/ZofzlkAojJ0ufiltD6KNRRh6Vde50yI3s3AFqA

/*eslint no-useless-computed-key: ["error", { "enforceForClassMembers": true }]*/

class Foo {
    ["foo"] = "bar";

    [0]() {}
    ['a']() {}
    get ['b']() {}
    set ['c'](value) {}

    static ["foo"] = "bar";

    static ['a']() {}
}

qVCMgjR5tKUNSBmRY5fDOf+I8msP/Eu4kA2GpuRUuMyrrB6hISlehS5gFUNCadklfNyIJdJp2ixhIGRO+KUcfupHIhiUrJdHmzO18ZZ7yHGxRy4ny17uW0hea4eRH5Gl

/*eslint no-useless-computed-key: ["error", { "enforceForClassMembers": true }]*/

class Foo {
    "foo" = "bar";

    0() {}
    'a'() {}
    get 'b'() {}
    set 'c'(value) {}

    static "foo" = "bar";

    static 'a'() {}
}

qVCMgjR5tKUNSBmRY5fDOf+I8msP/Eu4kA2GpuRUuMyrrB6hISlehS5gFUNCadklfNyIJdJp2ixhIGRO+KUcfvkwEEmLOtEUcor/AUvlM7WPCVTw2/VB3q62El54GQZX+akbX48yU1jkIcOtBm5Aaw==

/*eslint no-useless-computed-key: ["error", { "enforceForClassMembers": true }]*/

class Foo {
    ["constructor"]; // instance field named "constructor"

    "constructor"() {} // the constructor of this class

    ["constructor"]() {} // method named "constructor"

    static ["constructor"]; // static field named "constructor"

    static ["prototype"]; // runtime error, it would be a parsing error without `[]`
}

何时不使用

CHqe/S5cqldux92c2u/JNqj/mQQpQouIPZbIzvTPgbUsEqW/LYiEE5oPDZ7QQ4YSHKT0W7xVeuPbz0pvMgob1s5qLGNhDLcdnkc6RWrlfsGqJ6+75CYTA/0+9/hyk2jQf7ebqcC07z2l56RYTYklfA==