prefer-exponentiation-operator
禁止使用 Math.pow 以支持 ** 运算符
在 ES2016 中引入,中缀幂运算符 ** 是标准 Math.pow 函数的替代方式。
🌐 Introduced in ES2016, the infix exponentiation operator ** is an alternative for the standard Math.pow function.
中缀表示法被认为更具可读性,因此比函数表示法更可取。
🌐 Infix notation is considered to be more readable and thus more preferable than the function notation.
规则详情
🌐 Rule Details
此规则禁止调用 Math.pow,并建议改用 ** 操作符。
🌐 This rule disallows calls to Math.pow and suggests using the ** operator instead.
此规则的错误代码示例:
🌐 Examples of incorrect code for this rule:
/*eslint prefer-exponentiation-operator: "error"*/
const foo = Math.pow(2, 8);
const bar = Math.pow(a, b);
let baz = Math.pow(a + b, c + d);
let quux = Math.pow(-1, n);
符合此规则的正确代码示例:
🌐 Examples of correct code for this rule:
/*eslint prefer-exponentiation-operator: "error"*/
const foo = 2 ** 8;
const bar = a ** b;
let baz = (a + b) ** (c + d);
let quux = (-1) ** n;
选项
🌐 Options
此规则没有选项。
🌐 This rule has no options.
何时不使用
🌐 When Not To Use It
除非你的代码库支持 ES2016,否则不应使用此规则。
🌐 This rule should not be used unless ES2016 is supported in your codebase.
版本
此规则是在 ESLint v6.7.0 中引入。