What's the usage of rest parameter that will be added in ECMAScript 6?
For example, in ECMAScript 5 you can do the following to get an array of parameters starting from the second element:
// ES 5
store('Joe', 'money');
store('Jane', 'letters', 'certificates');
function store(name) {
var items = [].slice.call(arguments, 1); //['money'] in first case
items.forEach(function (item) {
vault.customer[name].push(item);
});
}
and that will be equivalent to the following code in ECMAScript 6:
// ES 6
store('Joe', 'money');
store('Jane', 'letters', 'certificates');
function store(name, ...items) {
items.forEach(function (item) {
vault.customer[name].push(items)
});
}
Is the difference between them is just syntax or there's a performance issue?
Also for spread operator (...)
//in ecmascript5
var max = Math.max.apply(null, [14, 3, 77]);
//but in ecmascript6
var max = Math.max(...[14, 3, 77]);
Is this just syntax change or performance issue?
Rest operator: The rest parameter is converse to the spread operator. while spread operator expands elements of an iterable, rest operator compresses them. It collects several elements. In functions when we require to pass arguments but were not sure how many we have to pass, the rest parameter makes it easier.
The main difference between rest and spread is that the rest operator puts the rest of some specific user-supplied values into a JavaScript array. But the spread syntax expands iterables into individual elements.
The rest parameter syntax allows a function to accept an indefinite number of arguments as an array, providing a way to represent variadic functions in JavaScript.
The JavaScript spread operator ( ... ) allows us to quickly copy all or part of an existing array or object into another array or object.
Is the difference between them is just syntax or there's a performance issue?
Both, and more...
Rest parameters:
slice
).If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With