版本

prefer-rest-params

要求使用剩余参数而不是 arguments

ES2015 中有剩余参数。我们可以使用该特性来处理可变参数函数,而不是 arguments 变量。

arguments 没有 Array.prototype 的方法,因此有点不方便。

规则详情

此规则旨在标记 arguments 变量的使用。

示例

此规则的 错误 代码示例

在游乐场中打开
/*eslint prefer-rest-params: "error"*/

function foo() {
    console.log(arguments);
}

function foo(action) {
    const args = Array.prototype.slice.call(arguments, 1);
    action.apply(null, args);
}

function foo(action) {
    const args = [].slice.call(arguments, 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 中引入。

资源

更改语言