2. Agenda
Introduction to Spread Syntax
Spread in function calls
Spread in array literals
Spread in object literals
Live Example
3. Introduction
Spread is an array / Object expression to be expand in places where zero
or more key-value pairs are expected.
Spread allows iterator over the arguments.
4. Spread in function calls
Spread is working as replacement of apply function.
Spread is used to pass an array of arguments to a function.
Syntax
functioncall ( …iterableObject );
5. Spread in function calls
Example
function getEmployeeDetails( id, name, designation ){
return id + “ ” + name + “ “ + designation;
}
var params = [ “emp01”, “PJ”, “SSE” ];
getEmployeeDetails( …params );
6. Spread in array literals
Array concatenation using spread literals.
Example
var arrOne = [ 1, 2, 3 ];
var arrayTwo = [ …arrOne, 4, 5, 6 ];
console.log( arrayTwo );
7. Spread in object literals
Array concatenation using spread literals.
Example
var objOne = [ “name” : “PJ” ];
var objTwo = [ “designation” : “SSE” ];
var cloneObj = { …objOne };
var mergeObj = { …objOne, …objTwo };
console.log( cloneObj ); console.log( mergeObj );