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;
何时不使用
¥When Not To Use It
除非你的代码库支持 ES2016,否则不应使用此规则。
¥This rule should not be used unless ES2016 is supported in your codebase.
版本
此规则是在 ESLint v6.7.0 中引入。