구조 분해 할당(Spread syntax, ...)
// For function calls:
myFunction(...iterableObj); // pass all elements of iterableObj as arguments to function myFunction
//For array literals:
[...iterableObj, '4', 'five', 6]; // combine two arrays by inserting all elements from iterableObj
//For object literals (new in ECMAScript 2018):
let objClone = { ...obj }; // pass all key:value pairs from an object
function sum(x, y, z) {
return x + y + z;
}
const numbers = [1, 2, 3];
console.log(sum(...numbers));
// expected output: 6
let numberStore = [0, 1, 2];
let newNumber = 12;
numberStore = [...numberStore, newNumber];
function myFunction(x, y, z) { }
let args = [0, 1, 2];
myFunction(...args);
function myFunction(v, w, x, y, z) { }
let args = [0, 1];
myFunction(-1, ...args, 2, ...[3]);
let parts = ['shoulders', 'knees'];
let lyrics = ['head', ...parts, 'and', 'toes'];
// ["head", "shoulders", "knees", "and", "toes"]
let arr1 = [0, 1, 2];
let arr2 = [3, 4, 5];
arr1 = [...arr1, ...arr2];
// arr1 is now [0, 1, 2, 3, 4, 5]
// Note: Not to use const otherwise, it will give TypeError (invalid assignment)