
prefer-exponentiation-operator
禁止使用 Math.pow
,建議使用 **
運算符
在 ES2016 中引入,中綴指數運算符 **
是標準 Math.pow
函數的替代方案。
中綴表示法被認為比函數表示法更易讀,因此更受偏好。
規則細節
此規則禁止呼叫 Math.pow
,並建議改用 **
運算符。
此規則的 錯誤 程式碼範例
在 Playground 中開啟
/*eslint prefer-exponentiation-operator: "error"*/
const foo = ;
const bar = ;
let baz = ;
let quux = ;
此規則的 正確 程式碼範例
在 Playground 中開啟
/*eslint prefer-exponentiation-operator: "error"*/
const foo = 2 ** 8;
const bar = a ** b;
let baz = (a + b) ** (c + d);
let quux = (-1) ** n;
何時不該使用
除非您的程式碼庫支援 ES2016,否則不應使用此規則。
版本
此規則在 ESLint v6.7.0 中引入。
延伸閱讀
