陣列解構
將family的值一一賦予到變數上
//原本寫法
let family = ['小明', '杰倫', '阿姨', '老媽', '老爸']
let ming = family[0];
let jay = family[1];
let auntie = family[2]
let mom = family[3];
let dad = family[4];
let family = ['小明', '杰倫', '阿姨', '老媽', '老爸']
//直接把變數放進陣列中
let [ming, jay, auntie, mom, dad] = family
若數量不對等,後面沒有的值就不會被複製過來
let [ming, jay, auntie] = family
//小明 杰倫 阿姨
若是中間的值沒給,記得留空值
let [ming, jay, , mom, dad] = family
//小明 杰倫 阿姨 老媽 老爸
物件解構
let Team = {
ming: '小明',
jay: '杰倫',
}
let { ming } = Team
//小明
let { ming: minggg } = Team //可以重新給變數一個名稱,並把值賦予到那個名稱上
console.log(minggg) //小明