const basicCookie = {
base: 'cookie',
madeIn: 'Korea',
}
const strawberryCookie = {
...basicCookie,
toping1: 'strawberry',
}
const chocoStrawberryCookie = {
...strawberryCookie,
toping2: 'chocolate',
}
console.log(basicCookie, strawberryCookie, chocoStrawberryCookie);
/* { base: 'cookie', madeIn: 'Korea' }
{ base: 'cookie', madeIn: 'Korea', toping1: 'strawberry' }
{ base: 'cookie', madeIn: 'Korea', toping1: 'strawberry', toping2: 'chocolate' }*/
const arr1 = [1, 2];
const arr2 = [3, 4];
const arr3 = [...arr1, ...arr2, 5]; // spread
const [num, ...arr4] = arr3; // rest
console.log(arr3); // [1, 2, 3, 4, 5]
console.log(num, arr4); // 1 [2, 3, 4, 5]
const arr = [1, 2, 3, 4, 5];
const arr2 = [6];
const [...arr3] = [...arr, ...arr2]; // rest, spread
console.log(arr3, ...arr3); // [1,2,3,4,5,6] 1 2 3 4 5 6
⭐ [...arr, ...arr2]는 이미 배열앞에 ...을 붙였기 때문에 [1, 2, 3, 4, 5, 6]
이 된다. spread연산이다.
⭐ 배열에 저장할때는 ...을 붙이면 rest연산이므로 오른쪽 배열이 그대로 arr3이라는 변수에 새로운 배열로 담긴다.