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
- Version
- 此规则是在 ESLint 2.0.0-alpha-1.Resources 中引入的
本文档系腾讯云开发者社区成员共同维护,如有问题请联系 cloudcommunity@tencent.com