首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

prefer-rest-params

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

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

规则细节

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

示例

此规则的错误代码示例:

代码语言:javascript
复制
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);
}

此规则的正确代码示例:

代码语言:javascript
复制
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 中引入的

扫码关注腾讯云开发者

领取腾讯云代金券