DAY 22
1
Modern Web

## 陣列

``````let family = ['小明', '杰倫', '阿姨', '老媽', '老爸'];
let ming = family[0];
let jay = family[1];
let auntie = family[2];
// ... 略

let [ming, jay, auntie, mom, papa] = family;
``````

``````let family = ['小明', '杰倫', '阿姨', '老媽', '老爸'];
let [ming, , , mom, papa] = family;
// ming: 小明
// mom: 老媽
// papa: 老爸
``````

``````let family = ['小明', '杰倫', '阿姨', '老媽', '老爸'];
let [, , , , , who] = family;
// who: undefined
``````

``````let Goku = '悟空';
let Ginyu = '基紐';
[Goku, Ginyu] = [Ginyu, Goku];
// Goku: '基紐'
// Ginyu: '悟空'
``````

``````let str = '基紐特攻隊';
[a, b, c, d, e] = str;
``````

## 物件解構

``````let family = {
ming: '小明',
jay: '杰倫',
};

// 一般會這樣寫
let ming = family.ming
let jay = family.jay

// 縮寫版
let { ming, jay } = family
// ming: 小明
// jay: 杰倫
``````

``````let GinyuTeam = {
Ginyu: '基紐',
Jeice: '吉斯',
burter: '巴特',
// ...
}
let { Ginyu: Goku } = GinyuTeam;
// Goku: '基紐'
``````

### 混合使用

``````let { ming: Goku, family: [, mom] } = { ming: '小明', family: ['阿姨', '老媽', '老爸'] }
console.log(Goku, mom); // 請問答案是什麼？
``````

• `ming` 取得右方的 '小明' 後，將變數名稱改為 `Goku`
• `family` 取得家庭陣列後，再將第二個值套用在 `mom` (第二個變數上。)

### 預設值

``````let [ming = '小明', jay = '杰倫'] = ['阿明']
// 第一個會被賦值，第二個會用預設
// ming: "阿明"
// jay: "杰倫"
``````

``````let { family: ming = '小明' } = {}
// ming: '小明'
``````

## 函式

``````function callSomeone ({person = '小明', person2}) {
console.log(`\${person} 呼叫 \${person2}`)
}
callSomeone({ person: '杰倫', person2: '阿姨' }); // 杰倫 呼叫 阿姨
callSomeone('杰倫', '阿姨'); // 小明 呼叫 undefined
callSomeone({ person2: '阿姨' }); // 小明 呼叫 阿姨
callSomeone({ person2: '杰倫', person: '啊罵' }); // 啊罵 呼叫 杰倫
callSomeone({}); // 小明 呼叫 undefined
``````