版本

prefer-exponentiation-operator

禁止使用 Math.pow,改用 ** 運算子

🔧 可修正

此規則報告的一些問題可透過 --fix 命令列選項自動修正

在 ES2016 中引入的中綴指數運算子 ** 是標準 Math.pow 函式的替代方案。

中綴表示法被認為比函式表示法更具可讀性,因此更受歡迎。

規則詳情

此規則禁止呼叫 Math.pow,並建議改用 ** 運算子。

此規則的不正確程式碼範例

在遊樂場中開啟
/*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);

此規則的正確程式碼範例

在遊樂場中開啟
/*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 中引入。

延伸閱讀

資源

變更語言