구조분해할당

트릴로니·2021년 12월 3일

자바스크립트

목록 보기
12/31

Destructuring allows you to easily extract array elements or object properties and store them in variables.

구조분해할당은 배열이나 객체의 요소를 뽑아서 그들의 변수에 저장할 수 있도록 한다

...spred는 모든 배열이나 객체의 요소를 꺼내서 새로운 배열이나 객체에 넣어주지만
구조분해할당은 하나의 요소만 변수에 저장?

[a, b]=['Hello', 'Max']
console.log(a) //Hello
console.log(b) //Max

{name} = {name: 'MAX, age: 28}
console.log(name) //MAX
console.log(age)//undefined


1,2를 배열에서 꺼냈고
1,3을 꺼내고 싶으면

이렇게 해주면 됨

객체의 구조분해 할당

구조분해할당은 함수의 매개변수에서 유용하게 쓰임 예를들어

객체에서 name만 필요한데 전체 객체를 받음

이렇게 쓸 수 있음
객체에서 name프로퍼니만 꺼내서 매개변수에 넣어줄 수 있음

0개의 댓글