<script>
let arr = ["Bora", "Lee"]; //이름과 성을 요소로 가진 배열
let [firstName, surname] = arr; //구조 분해 할당을 이용함
const[f,s] = arr; //상수로 대입 받기도 가능
/*let firstName = arr[0];
let surname = arr[1];*/ //위 코드와 같음
console.log(firstName); //Bora
console.log(surname); //Lee
</script>
<script>
let [firstName, surname] = "Bora Lee" .split(' ');
//split 메서드 호출 결과로 배열 반환, 배열을 바로 변수로 대입
</script>
<script>
let user ={ };
[user.name, user.surname] = "Bora Lee" .split(' ');
console.log(user.name); //Bora
console.log(user.surname); //Lee
</script>
<script>
let [firtsName, ,lastName] = ["Jae","Jeon","Lim"];
console.log(firstName); //Jae
console.log(lastName); //Lim
</script>
<script>
let [firtsName,middleName, ...rest] = ["Jae","Jeon","Lim","is my name"];
console.log(firstName); //Jae
console.log(middleName); //Jeon
//rest는 배열
console.log(rest[0]); //Lim
console.log(rest[1]); //is my name];
console.log(rest.length); //2
</script>
<script>
let[firstName,surname] = []; //대입할 값이 없으므로 undefined 값이 나옴
console.log(firstName); //undefined
console.log(surname); //undefined
</script>
<script>
let [name = "Guest", surname = "Anonymous", title="MyName"] = ["JaeYeon","Lim"];
console.log(name); // JaeYeon (배열에서 받아온 값)
console.log(surname); // Lim (배열에서 받아온 값)
console.log(title); // MyName (기본값)
</script>
구조 분해 할당 문법을 이용하여 객체도 분해 가능
할당 연산자 우측에는 분해 할 객체를, 좌측에는 상응하는 객체 프로퍼티의 "패턴"을 입력
<script>
let option = {title:"Menu", width: 100, height:200}; //let 괄호 안의 변수순서가 바뀌어도 동일하게 동작
let {title, width, height} = option;
console.log(title); //menu
console.log(width); //100
console.log(height); //200
</script>
<script>
let option = {title:"Menu", width: 100, height:200};
let {width:w, height: h, title} = option; //width는 w변수에 height는 h변수에 title은 그대로 title변수에 저장
console.log(title); //menu
console.log(w); //100
console.log(h); //200
</script>
<script>
let option = {title:"Menu"};
let {title, width = 100, height = 200} = option; // =을 이용해 width,height 기본값 지정
console.log(title); //menu
console.log(width); //100
console.log(height); //200
</script>
<script>
let option = {title:"Menu"};
let {title, w = 100, h = 200} = option; // =을 이용해 width,height 기본값 지정
console.log(title); //menu
console.log(w); //100
console.log(h); //200
</script>
<script>
let option = {title:"Menu", width: 100, height:200};
let {title, ...rest} = option;
console.log(title); //menu
console.log(rest.width); //100
console.log(rest.height); //200
//rest에는 객체가 할당됨
</script>