Javascript - spread 연산자

이진아·2021년 1월 9일
0

JAVASCRIPT

목록 보기
6/9
post-thumbnail

javascript에서 스프레드연산자를 안다면 배열을 사용할때 간편하게 사용 할 수 있어서 좋다.

1.정의

스프레드 연산자는 자신이 원하는 객체를 복사해서 넣는 방법이다.
사용방법은 앞에 ... 을 붙이면 된다.

2. 코드

어떻게 복사해서 넣는지는 직접 코드를 확인하면서 보면 쉽게 이해할 수 있다.

const student1 ={
    name: '이'
};

const student2 = {
    ...student1,
    name: '김',
    age: 10
};

const student3 = {
	...student2
    name: '박',
    age:20,
    hobby: 'game'
};

console.log(student1);
console.log(student2);
console.log(student3);

위의 코드를 해석을 해석하자면
student2에서 ...student1를 작성하므로써 스프레드 연산자를 사용하였다.
그래서 출력을 하였을 때 student1 객체들과 함께 출력이 된다.

student3도 마찬가지로 ...student2을 썼기때문에 출력을 할때 student2 객체들도 함께 출력이 된다.

profile
새싹 개발자><

0개의 댓글