[ES6]解構(gòu)賦值

在ES6中新增了變量賦值的方式。

數(shù)組解構(gòu)賦值

按照一定的模式,從數(shù)組和對(duì)象中提取值。

let [a, b, c] = [1, 2, 3]

解構(gòu)賦值的重點(diǎn)在于和左側(cè)要和右側(cè)有類似的格式,例如上面的中括號(hào)[ ]

賦值元素可以是任意可遍歷元素

let [a, b, c] = "abc" // ["a", "b", "c"]
let [one, two, three] = new Set([1, 2, 3])

可以跳過(guò)賦值元素

如果想跳過(guò)數(shù)組的某個(gè)元素進(jìn)行賦值,可以使用逗號(hào)實(shí)現(xiàn)。

let [name, , title] = ['John', 'Jim', 'Sun', 'Moon']

console.log( title ) // Sun

使用rest獲取剩下的數(shù)組元素

let [name1, name2, ...rest] = ["Julius", "Caesar", "Consul", "of the Roman Republic"]

console.log(name1) // Julius
console.log(name2) // Caesar

// Note that type of `rest` is Array.
console.log(rest[0]) // Consul
console.log(rest[1]) // of the Roman Republic
console.log(rest.length) // 2

注意:要保證rest放在最后的位置。

默認(rèn)值

如果對(duì)應(yīng)位置沒(méi)有值,則取默認(rèn)值。

let [name = "Guest", surname = "Anonymous"] = ["Julius"]

console.log(name)    // Julius (from array)
console.log(surname) // Anonymous (default used)

對(duì)象解構(gòu)賦值

同樣我們?cè)?左側(cè)要有和右側(cè)類似的格式。

let options = {
  title: "Menu",
  width: 100,
  height: 200
}

let {title, width, height} = options

console.log(title)  // Menu
console.log(width)  // 100
console.log(height) // 200

注:屬性的順序無(wú)需一致。

改變獲取屬性名

對(duì)象的屬性名: 自定義變量名

let {width: w, height: h, title} = options

注:當(dāng)我們自定義變量名后,默認(rèn)的變量名將不可用。

默認(rèn)值

對(duì)應(yīng)位置沒(méi)有值時(shí),取默認(rèn)值。

let options = {
  title: "Menu"
}

let {width = 100, height = 200, title} = options

console.log(title)  // Menu
console.log(width)  // 100
console.log(height) // 200

rest運(yùn)算符

存儲(chǔ)剩余屬性,rest為對(duì)象。

let options = {
  title: "Menu",
  height: 200,
  width: 100
}

let {title, ...rest} = options

// now title="Menu", rest={height: 200, width: 100}
console.log(rest.height)  // 200
console.log(rest.width)   // 100

嵌套對(duì)象

如果一個(gè)Array或者Object比較復(fù)雜,嵌套了很多層,那么和之前一樣,在=左邊與右邊的結(jié)構(gòu)一致即可。

let options = {
  size: {
    width: 100,
    height: 200
  },
  items: ["Cake", "Donut"],
  extra: true    // something extra that we will not destruct
}

// destructuring assignment on multiple lines for clarity
let {
  size: { // put size here
    width,
    height
  },
  items: [item1, item2], // assign items here
  title = 'Menu' // not present in the object (default value is used)
} = options

console.log(title)  // Menu
console.log(width)  // 100
console.log(height) // 200
console.log(item1)  // Cake
console.log(item2)  // Donut

注意:此處的size和items僅為結(jié)構(gòu),不是變量。若要保存對(duì)應(yīng)的size,可以這樣。

let {
  size},
  items: [item1, item2],
  title = 'Menu'
} = options
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 一、函數(shù)的默認(rèn)參數(shù) ES6 之前,不能直接為函數(shù)的參數(shù)指定默認(rèn)值,只能采用變通的方法。 ES6 允許為函數(shù)的參數(shù)設(shè)...
    _ClariS_閱讀 698評(píng)論 0 2
  • 引入 在ES5中,開(kāi)發(fā)者們?yōu)榱藦膶?duì)象和數(shù)組中獲取特定數(shù)據(jù)并賦值給變量,編寫了許多看起來(lái)同質(zhì)化的代碼 這段代碼從op...
    nengzhuan_zhang閱讀 646評(píng)論 0 0
  • 前面的話 我們經(jīng)常定義許多對(duì)象和數(shù)組,然后有組織地從中提取相關(guān)的信息片段。在ES6中添加了可以簡(jiǎn)化這種任務(wù)的新特性...
    sunnyghx閱讀 825評(píng)論 0 0
  • 前言 為什么要使用解構(gòu)賦值,ES5及以前的版本 雖然這段代碼看上去也挺簡(jiǎn)單的,但想象一下如果你要給大量的變量賦值,...
    平凡_ee89閱讀 196評(píng)論 0 0
  • 數(shù)組的解構(gòu)賦值 ES6 允許按照一定模式,從數(shù)組和對(duì)象中提取值,對(duì)變量進(jìn)行賦值,這被稱為解構(gòu)(Destructur...
    _羊羽_閱讀 623評(píng)論 0 1

友情鏈接更多精彩內(nèi)容