no-magic-numbers

禁止幻数

'Magic numbers' 是在代码中多次出现但没有明确含义的数字。最好用命名常量替换它们。

var now = Date.now(),
    inOneHour = now + (60 * 60 * 1000);

规则详情

no-magic-numbers 规则旨在通过确保将特殊数字声明为常量以使其含义明确,从而使代码更具可读性和重构更容易。

此规则的错误代码示例:

/*eslint no-magic-numbers: "error"*/

var dutyFreePrice = 100,
    finalPrice = dutyFreePrice + (dutyFreePrice * 0.25);
/*eslint no-magic-numbers: "error"*/

var data = ['foo', 'bar', 'baz'];

var dataLast = data[2];
/*eslint no-magic-numbers: "error"*/

var SECONDS;

SECONDS = 60;

此规则的正确代码示例:

/*eslint no-magic-numbers: "error"*/

var TAX = 0.25;

var dutyFreePrice = 100,
    finalPrice = dutyFreePrice + (dutyFreePrice * TAX);

选项

ignore

yfZ1WVy3j3gfQSs8XONiiHtPPQoePB4pauD+dWKLnvIaTGi+Ny6uedhQbC9apGojSKux93uYblnkYos0uOkJbY8x4bOKAYntK/9L/ZSZ2O+iU64U+SKtGUF05aNaCZSerSEyoc4EjZNSKXR73L5FCw==

ZW2cT1hnvOUJG/K429IrvshVU+gTRxqrBu0jVHwJZEHmM+1ktevlGc79wSEMrfUVKLXS2ELqiQQsP2rgjsWZEhYAoN/m8bpx2veeMVB3Qc7jRFITA8KIzAtP4Sv/MHhIR9fMdmJP9B23IeWMNvMULXzlTd5EuPQBU+RLKA22DbtZTS5Ji9yHG7WlMN/kl6bLwMyhXwNFEVYUckhAdZF7nyOZtoKu90vRlZsUKHLSIYDqNEH74tMgj4rnr2kuE/CO

kCWQAILfLXiNSqYJMMPhUopsMAlhqD/J33CHeLxz+89B5mu/qz/as6o8IZyKrUmxa84x9BWn4d1sHvKnJrZ98rfVq27PBq6Cy2IBbfmZXIg=

/*eslint no-magic-numbers: ["error", { "ignore": [1] }]*/

var data = ['foo', 'bar', 'baz'];
var dataLast = data.length && data[data.length - 1];

kCWQAILfLXiNSqYJMMPhUrLrksOM9mmCiKpqvDKps3Qiue9C7oy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JXhBjTEO7Z80BfXyzRbZSJrv0x8kdP0YK3z2PAvVo5w05wJbDtqqEfi5bgPEu4l5Ep84DVArK3UaIolqkBNFp23oc7i9jEbEO358lQ8uJ36S6jxtWzJ/OHb3L6+ITAfp+4kRc/gPqw1SK73fk1wEyFy3ZdW69BcPoiZo+gPGUg2B31tHxQMdCJzHsVDPEawy/TnBXZEaVkL9/CdQ58IGJKjxIjO10LUa2a6CfPEeXQybSHCgtOxHdVqpeqY/U=

gzQzd7+5yIYq6V/FaZCnIO617T8fCkEl5bGB50S9YIrPnxmOA8FD+vC2Yrm2qf5eIyvVUm9TRiW55oe1dzBW1jVntGCjrmjmrsdjmVJqgc0=

/*eslint no-magic-numbers: ["error", { "ignoreArrayIndexes": true }]*/

var item = data[2];

data[100] = a;

f(data[0]);

a = data[-0]; // same as data[0], -0 will be coerced to "0"

a = data[0xAB];

a = data[5.6e1];

a = data[10n]; // same as data[10], 10n will be coerced to "10"

a = data[4294967294]; // max array index

gzQzd7+5yIYq6V/FaZCnIO617T8fCkEl5bGB50S9YIrPnxmOA8FD+vC2Yrm2qf5eVxS2ipVJD9jhqdTINp05hn3skbSYshYy/EDcs5W7+hs=

/*eslint no-magic-numbers: ["error", { "ignoreArrayIndexes": true }]*/

f(2); // not used as array index

a = data[-1];

a = data[2.5];

a = data[5.67e1];

a = data[-10n];

a = data[4294967295]; // above the max array index

a = data[1e500]; // same as data["Infinity"]

ignoreDefaultValues

Xj+CW7USS+Fqy4ivL7VQ21aHvwjasero99lHQfB7NXCzTv/+1NIx8H378iJ+Qt/EEc6gQewWRWcU5RXivmJ86mNtBbQF/i3lz1m0nmvbrLc2Xu/pocYFgMwdXAqMb0SB1H/69FjWVz+Mdk8fF4IQBGolvtd1yATrrj3fTpAxUO0=

jniuN3mg4C4qyHVbNofXIPwHDFhdD2zbPo975FGxAkV9bQPcKVa9gJUE/yG6KwasvRmN4nq9L3jp0bYhouWx1zN+0VsolZMaecl+JziEYWw=

/*eslint no-magic-numbers: ["error", { "ignoreDefaultValues": true }]*/

const { tax = 0.25 } = accountancy;

function mapParallel(concurrency = 3) { /***/ }
/*eslint no-magic-numbers: ["error", { "ignoreDefaultValues": true }]*/

let head;
[head = 100] = []

ignoreClassFieldInitialValues

Xj+CW7USS+Fqy4ivL7VQ294KgFAXksWviPcPNN+gGjmeJYIn2mYAW9IvC1GY+MHwdHKpEuHqMDGBnGBJV25BIALZbP/VOYI3/PDSh6Oqrq1TKPBJEmxU96jFQlN96cUtkOfuoQbEJ3kdAQmc8SVf6iqCruGtSjAR83b5P5zWiew=

15S6nGNKDImJDcIRYwzSDrqd7v7+ngDhI03Fo+74NqUOpc+nUQLLIl45/cTtltpyndVxF/pF8Tx9qKIit0406zrm3fUSavBF7COSwHyMi6uxqC6aQApP+GwF8vOaHrDt

/*eslint no-magic-numbers: ["error", { "ignoreClassFieldInitialValues": true }]*/

class C {
    foo = 2;
    bar = -3;
    #baz = 4;
    static qux = 5;
}

15S6nGNKDImJDcIRYwzSDrqd7v7+ngDhI03Fo+74NqUOpc+nUQLLIl45/cTtltpyndVxF/pF8Tx9qKIit04067VA5RRIodEnIt6Wd7WD1W7G50VK6A55Kj2zdgvSh2j0

/*eslint no-magic-numbers: ["error", { "ignoreClassFieldInitialValues": true }]*/

class C {
    foo = 2 + 3;
}

class D {
    2;
}

enforceConst

Xj+CW7USS+Fqy4ivL7VQ21d/XnGedLTJK9GenH92GjJ0zwgwsIDxfTmDMi4vVxe3/YEtdRwOYrRxtifBvYB6Dyc7lnVahcTdwvyeJeuKCoB9pq7YBiOUlZsjG7g9KHJ6zthYKVdSsmyJFT0La7HxGD7HXbS0JU0tEl07MGQTuK/CCorQyPi8nT+h1f2u3YMs

/STg9vmPl7TmjtpBlYhcT1/4OvXY1n1RiFQc7KC1jytqG8X7pn2S60x3j/Moi01WxQ9nflRnbmLCPmMtB3wlUbq98O8bgSPhwdqY72D/hd8=

/*eslint no-magic-numbers: ["error", { "enforceConst": true }]*/

var TAX = 0.25;

var dutyFreePrice = 100,
    finalPrice = dutyFreePrice + (dutyFreePrice * TAX);

detectObjects

Xj+CW7USS+Fqy4ivL7VQ21d/XnGedLTJK9GenH92GjJ0zwgwsIDxfTmDMi4vVxe3DpLr6zo+KI0Jmv/cNVvqUMdg8q6fMLKkDiyJImIJXL4mujA5+pgSkIl0lvT4XegxFrFOVXnCBut3fTkQ/ogB62g3AzMALW+vWeYuKHilCak=

RORCc3cUjk1gRadf9McQKfmuEpN8Qjmus4kNpePfXfgLaKeAUJCfG3yfzeWk4HcwL5IUOpTR3ADL5bXtZBreSeU8LEg+jdkVGnMG9fyi998=

/*eslint no-magic-numbers: ["error", { "detectObjects": true }]*/

var magic = {
  tax: 0.25
};

var dutyFreePrice = 100,
    finalPrice = dutyFreePrice + (dutyFreePrice * magic.tax);

RORCc3cUjk1gRadf9McQKfmuEpN8Qjmus4kNpePfXfgLaKeAUJCfG3yfzeWk4HcwV2HzJ0hcGdQlhApupu3wzhxAQ/AGnwntWbgmEwEG//Q=

/*eslint no-magic-numbers: ["error", { "detectObjects": true }]*/

var TAX = 0.25;

var magic = {
  tax: TAX
};

var dutyFreePrice = 100,
    finalPrice = dutyFreePrice + (dutyFreePrice * magic.tax);