Web
[JS] Spread 구문
RealJuney
2023. 6. 23. 04:03
Spread 구문은 spread 하고자 하는 배열 앞에 ...을 써주면 된다.
const arr = ['a', 'b', 'c'];
const arr2 = [...arr, 'd'];
// arr2: ['a', 'b', 'c', 'd']
함수 호출 시 argument로 전달 할 때도 편리하게 사용 가능하다.
function f(a, b, c) {
console.log(a);
console.log(b);
console.log(c);
}
const arr = [2, 1, 4];
f(...arr);
// output:
// 2
// 1
// 4
ES2018에서 부턴 객체도 spread가 가능하다.
const A = {
name: 'Juney',
};
const B = {
...Juney,
age: 22,
};
// B: {name: 'Juney', age: 22}