10 Before ES2015, one must use `Function.prototype.apply()` to call variadic functions.
13 var args = [1, 2, 3, 4];
14 Math.max.apply(Math, args);
17 In ES2015, one can use spread syntax to call variadic functions.
22 var args = [1, 2, 3, 4];
28 This rule is aimed to flag usage of `Function.prototype.apply()` in situations where spread syntax could be used instead.
32 Examples of **incorrect** code for this rule:
37 /*eslint prefer-spread: "error"*/
39 foo.apply(undefined, args);
40 foo.apply(null, args);
41 obj.foo.apply(obj, args);
46 Examples of **correct** code for this rule:
51 /*eslint prefer-spread: "error"*/
53 // Using spread syntax
57 // The `this` binding is different.
59 obj.foo.apply(null, args);
60 obj.foo.apply(otherObj, args);
62 // The argument list is not variadic.
63 // Those are warned by the `no-useless-call` rule.
64 foo.apply(undefined, [1, 2, 3]);
65 foo.apply(null, [1, 2, 3]);
66 obj.foo.apply(obj, [1, 2, 3]);
73 This rule analyzes code statically to check whether or not the `this` argument is changed. So, if the `this` argument is computed in a dynamic expression, this rule cannot detect a violation.
76 /*eslint prefer-spread: "error"*/
79 a[i++].foo.apply(a[i++], args);
81 // This does not warn.
82 a[++i].foo.apply(a[i], args);
87 This rule should not be used in ES3/5 environments.
89 In ES2015 (ES6) or later, if you don't want to be notified about `Function.prototype.apply()` callings, you can safely disable this rule.