prefer-spread

需要扩展运算符而不是 .apply()

在 ES2015 之前,必须使用 Function.prototype.apply() 来调用可变参数函数。

var args = [1, 2, 3, 4];
Math.max.apply(Math, args);

在 ES2015 中,可以使用扩展语法来调用可变参数函数。

/*eslint-env es6*/

var args = [1, 2, 3, 4];
Math.max(...args);

规则详情

此规则旨在在可以使用扩展语法的情况下标记 Function.prototype.apply() 的使用。

示例

此规则的错误代码示例:

/*eslint prefer-spread: "error"*/

foo.apply(undefined, args);
foo.apply(null, args);
obj.foo.apply(obj, args);

ZV3WTnsA6mjxcovKoGG8hTv7+Ha1z2USrMCyxcmIv+4859h0MFN8WvPgRtljmRMU

/*eslint prefer-spread: "error"*/

// Using spread syntax
foo(...args);
obj.foo(...args);

// The `this` binding is different.
foo.apply(obj, args);
obj.foo.apply(null, args);
obj.foo.apply(otherObj, args);

// The argument list is not variadic.
// Those are warned by the `no-useless-call` rule.
foo.apply(undefined, [1, 2, 3]);
foo.apply(null, [1, 2, 3]);
obj.foo.apply(obj, [1, 2, 3]);

Dfb9LToCAF8B2h2A8Bxn7A==

jVe8Tqw7vRIgz/noRrEPuWHDErCwtJ8n9bhiUcn+/Q221spqOiBFhusX2fc5OoqVmKtDDyICgzEi30mPf5pWwSu/WesAM07wpdQMBZeR876INXfzIE8Bx9WisinUowP692eYoWaMHxiuf0GwOMcXgCwkfQ+6cKbaETvcReLUtCteqGlA9Q/vumbVAgkbc66PiQBN5EkWoE9JtLNpmSp0JovdCJW550gPQIYFcLejCvZJsUzCoASpM+4A/taumc0J

/*eslint prefer-spread: "error"*/

// This warns.
a[i++].foo.apply(a[i++], args);

// This does not warn.
a[++i].foo.apply(a[i], args);

何时不使用

fzJOzq00XqqbtzHWkKvVLj4q3QxJrs8CQ8fhVRVh2JjPPfpsDwc6Dbvn6DdohEWn

WhGR9cbeUbVH1P+dak21aBp7+pSk0bD7ziGMs0RSVITPRd6KlB135JjyhXuvK4vhnY+LzF5X7n/cEQw0Kq/91EWZlneIrJ1liBHo25MED0h8PoWop2lkKTlTQP6VpWm8saJ9s7q3DYrUJn1IoA7vA/r/mABoiMgjUDGupEVQ1jyv+qkPwrJWKyAJVoU141CN/4T5jeuYkWh5bf2LKqDOkHSISIVilS6KvhrfAoEUnuQ=