Start a personal dev blog on your domain for free with Hashnode and grow your readership.
Rest and Spread operators in ES6
Anonymous
const Mathlib = {
multiply(...abc){
return this.add(...abc)// what argument it is passing to add function array or individual value
},
add(p,q,r){
return p+q+r;
}
}
Mathlib.multiply(2,4,5);
above code is working fine ,so I am thinking if first time ....abc will store all value in array i.e when we are calling Mathlib.multiply(2,4,5) and second time when we are calling add(...abc) then also it should pass the array but it is passing individual value and thats why output is coming as 11.
So if above scenario is meaningful then below code should not produce error:
var a =[1,2,3];
...a ; // error
I am unable to understand what is going on under the hood??
Cho, Yongjoon
Apr 10, 2018
The operator
...
cannot be used in anywhere.Rest Parameters
Only available in function parameters to "represent an indefinite number of arguments as an array".
function f(a, b, ...theArgs) { // ... }
Spread Syntax
3 Cases are available
1) For function calls:
myFunction(...iterableObj);
2) For array literals or strings:
[...iterableObj, '4', 'five', 6];
3) For object literals (new in ECMAScript 2018):
let objClone = { ...obj };