prefer-rest-params
要求使用 rest 參數而非 arguments
ES2015 中有 rest 參數。我們可以對可變參數函式使用此功能,而不是 arguments
變數。
arguments
沒有 Array.prototype
的方法,因此有點不方便。
規則詳情
此規則旨在標記 arguments
變數的使用。
範例
此規則的 **錯誤** 程式碼範例
在遊樂場開啟
/*eslint prefer-rest-params: "error"*/
function foo() {
console.log();
}
function foo(action) {
const args = Array.prototype.slice.call(, 1);
action.apply(null, args);
}
function foo(action) {
const args = [].slice.call(, 1);
action.apply(null, args);
}
此規則的 **正確** 程式碼範例
在遊樂場開啟
/*eslint prefer-rest-params: "error"*/
function foo(...args) {
console.log(args);
}
function foo(action, ...args) {
action.apply(null, args); // or `action(...args)`, related to the `prefer-spread` rule.
}
// Note: the implicit arguments can be overwritten.
function foo(arguments) {
console.log(arguments); // This is the first argument.
}
function foo() {
const arguments = 0;
console.log(arguments); // This is a local variable.
}
何時不該使用
此規則不應在 ES3/5 環境中使用。
在 ES2015 (ES6) 或更新版本中,如果您不想收到關於 arguments
變數的通知,則可以安全地停用此規則。
相關規則
版本
此規則在 ESLint v2.0.0-alpha-1 中引入。