DAY 26
0
Modern Web

## 解構賦值方便在哪裡?

``````let JK = {
name: 'JKchan',
age: 16
}
let name = JK.name;
let age = JK.age;
``````

``````let JK = {
name: 'JKchan',
age: 16
}
let { name, age } = JK;
console.log(name, age); // JKchan 16
``````

`name``age` 直接取出 `JK.name``JK.age` 得值賦予在他們自己身上

## 賦值中解構

``````let JK = {
name: 'JKchan',
age: 16
}
let name = "JKsama";
let age = 18;
({ name, age } = JK);
console.log(name, age); // JKchan 16
``````

## 預設值

``````let JK = {
name: 'JKchan',
age: 16
}
let { name, age, address } = JK;
console.log(name, age, address); // JKchan 16 undefined
``````

``````let { name, age, address = 'Tokyo'} = JK;
console.log(name, age, address); // JKchan 16 Tokyo
``````

## 陣列型式

``````let JK = ['JKchan', 16];
let [name, age] = JK;
console.log(name, age); // JKchan 16
``````

``````let JK = ['JKchan', 16];
let [,age] = JK;
console.log(age); // 16
``````

``````let JK = ['JKchan', 16];
let name = JKsama;
let age = 18;
[name, age] = JK;
console.log(name, age); // JKchan 16
``````

``````let JK = ['JKchan'];
let [name, age = 16] = JK;
console.log(name, age); // JKchan 16
``````

30天前端網頁技術超入門介紹30