What is the meaning of "foo(...arg)" (three dots in a function call)?

It is a JavaScript feature called Rest Parameters. By using it your function can take any number of arguments. You put the three dots just before the argument (without a whitespace character) and the mechanism spreads it for you as if it was a list of several arguments. In Eloquent Javascript you have a good example of it.

let numbers = [5, 1, 7];
console.log(max(...numbers));
// -> 7

This has nothing to do with jQuery or Angular. It's a feature that was introduced in ES2015.

This particular use of ... doesn't actually have an official name. A name that is in line with other uses would be "spread argument" (a generic term would be "spread syntax"). It "explodes" (spreads) an iterable and passes each value as argument to the function. Your example is equivalent to:

this.heroes.push.apply(this.heroes, Array.from(heroes));

Besides being more concise, another advantage of ... here is that it can be more easily used with other concrete arguments:

func(first, second, ...theRest);

// as opposed to the following or something similar:
 func.apply(null, [first, second].concat(Array.from(heroes)));

Tags:

Javascript