27 lines
1.1 KiB
Plaintext
27 lines
1.1 KiB
Plaintext
== Why is this an issue?
|
|
|
|
The spread operator is a more concise and more readable way to pass arguments to a function that takes a variable number of arguments (variadic function). Prior to ES2015, the only way to call such functions with a variable number of arguments was to use the `.apply()` method.
|
|
|
|
[source,text,diff-id=1,diff-type=noncompliant]
|
|
----
|
|
foo.apply(undefined, args); // Noncompliant: use spread syntax instead of .apply()
|
|
foo.apply(null, args); // Noncompliant: use spread syntax instead of .apply()
|
|
obj.foo.apply(obj, args); // Noncompliant: use spread syntax instead of .apply()
|
|
----
|
|
|
|
Using `.apply()` is no longer necessary in such cases - replace it with a spread operator applied to the array of arguments.
|
|
|
|
[source,text,diff-id=1,diff-type=compliant]
|
|
----
|
|
foo( ...args);
|
|
foo( ...args);
|
|
obj.foo( ...args);
|
|
----
|
|
|
|
|
|
== Resources
|
|
=== Documentation
|
|
|
|
* MDN web docs - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax#spread_in_function_calls[spread syntax]
|
|
* MDN web docs - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply[``apply()``]
|