prefer-rest-params
要求使用 rest 參數而不是 arguments
在 ES2015 中有 rest 參數。我們可以將該功能用於可變參數函式,而不是 arguments
變數。
arguments
沒有 Array.prototype
的方法,因此有點不方便。
規則詳情
此規則旨在標記 arguments
變數的使用。
範例
此規則的不正確程式碼範例
在線上編輯器中開啟
/*eslint prefer-rest-params: "error"*/
function foo() {
console.log();
}
function foo(action) {
var args = Array.prototype.slice.call(, 1);
action.apply(null, args);
}
function foo(action) {
var 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() {
var arguments = 0;
console.log(arguments); // This is a local variable.
}
何時不應使用
此規則不應在 ES3/5 環境中使用。
在 ES2015 (ES6) 或更新版本中,如果您不想收到關於 arguments
變數的通知,則可以安全地停用此規則。
相關規則
版本
此規則在 ESLint v2.0.0-alpha-1 中引入。