prefer-rest-params
需要 rest 参数而不是 arguments
ES2015 中有剩余参数。我们可以使用这个特性来实现可变参数函数,而不是使用 arguments 变量。
🌐 There are rest parameters in ES2015.
We can use that feature for variadic functions instead of the arguments variable.
arguments 没有 Array.prototype 的方法,所以有点不方便。
规则详情
🌐 Rule Details
此规则旨在标记 arguments 变量的使用。
🌐 This rule is aimed to flag usage of arguments variables.
此规则的错误代码示例:
🌐 Examples of incorrect code for this rule:
/*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);
}
符合此规则的正确代码示例:
🌐 Examples of correct code for this rule:
/*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.
}
选项
🌐 Options
此规则没有选项。
🌐 This rule has no options.
何时不使用
🌐 When Not To Use It
此规则不应在 ES3/5 环境中使用。
🌐 This rule should not be used in ES3/5 environments.
在 ES2015(ES6)或更高版本中,如果你不想收到关于 arguments 变量的通知,那么禁用此规则是安全的。
🌐 In ES2015 (ES6) or later, if you don’t want to be notified about arguments variables, then it’s safe to disable this rule.
相关规则
版本
此规则是在 ESLint v2.0.0-alpha-1 中引入。