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.
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.
foo( ...args);
foo( ...args);
obj.foo( ...args);