Eslint
规则 | Rules

prefer-rest-params

建议使用其余参数而不是arguments(prefer-rest-params)

ES2015中还有其他参数。我们可以使用该特性来代替arguments可变参数。

arguments没有Array.prototype方法,所以这有点不方便。

规则细节

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

示例

此规则的错误代码示例:

function foo() { console.log(arguments } function foo(action) { var args = Array.prototype.slice.call(arguments, 1 action.apply(null, args } function foo(action) { var args = [].slice.call(arguments, 1 action.apply(null, args }

此规则的正确代码示例:

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变量的通知,那么禁用此规则是安全的。

相关规则

  • prefer-spread