深入理解ES6之?dāng)?shù)據(jù)解構(gòu)的用法
一 對(duì)象解構(gòu)
對(duì)象解構(gòu)語(yǔ)法在賦值語(yǔ)句的左側(cè)使用了對(duì)象字面量
let node = { type: true, name: false } //既聲明又賦值 let { type, name } = node; //或者先聲明再賦值 let type, name ({type,name} = node); console.log(type);//true console.log(name);//false
type與name標(biāo)識(shí)符既聲明了本地變量,也讀取了對(duì)象的相應(yīng)屬性值。
解構(gòu)賦值表達(dá)式的值為表達(dá)式右側(cè)的值。當(dāng)解構(gòu)表達(dá)式的右側(cè)的計(jì)算結(jié)果為null或者undefined時(shí),會(huì)拋出錯(cuò)誤。
默認(rèn)值
當(dāng)你使用解構(gòu)賦值語(yǔ)句時(shí),如果所指定的本地變量在對(duì)象中沒有找到同名屬性,那么該變量會(huì)被賦值為undefined
let node = { type: true, name: false }, type, name, value; ({type,value,name} = node); console.log(type);//true console.log(name);//false console.log(value);//undefined
你可以選擇性地定義一個(gè)默認(rèn)值,以便在指定屬性不存在時(shí)使用該值。
let node = { type: true, name: false }, type, name, value; ({ type, value = true, name } = node); console.log(type);//true console.log(name);//false console.log(value);//true
賦值給不同的本地變量名
let node = { type: true, name: false, value: "dd" } let { type: localType, name: localName, value: localValue = "cc" } = node; console.log(localType); console.log(localName); console.log(localValue);
type:localType這種語(yǔ)法表示要讀取名為type的屬性,并把它的值存儲(chǔ)在變量localType上。該語(yǔ)法與傳統(tǒng)對(duì)象字面量的語(yǔ)法相反
嵌套的對(duì)象結(jié)構(gòu)
let node = { type: "Identifier", name: "foo", loc: { start: { line: 1, column: 1 }, end: { line: 1, column: 4 } } } let { loc: localL, loc: { start: localS, end: localE } } = node; console.log(localL);// start: {line: 1,column: 1},end: {line: 1,column: 4} console.log(localS);//{line: 1,column: 1} console.log(localE);//{line: 1,column: 4}
當(dāng)冒號(hào)右側(cè)存在花括號(hào)時(shí),表示目標(biāo)被嵌套在對(duì)象的更深一層中(loc: {start: localS,end: localE})
二 數(shù)據(jù)解構(gòu)
數(shù)組解構(gòu)的語(yǔ)法看起來(lái)跟對(duì)象解構(gòu)非常相似,只是將對(duì)象字面量換成了數(shù)組字面量。
let colors = ["red", "blue", "green"]; let [firstC, secondC, thirdC, thursC = "yellow"] = colors; console.log(firstC//red console.log(secondC);//blue console.log(thirdC);//green console.log(thursC);//yellow
你也可以在解構(gòu)模式中忽略一些項(xiàng),并只給感興趣的項(xiàng)提供變量名。
let colors = ["red","green","blue"]; let [,,thirdC] = colors; console.log(thirdC);//blue
thirdC之前的逗號(hào)是為數(shù)組前面的項(xiàng)提供的占位符。使用這種方法,你就可以輕易從數(shù)組任意位置取出值,而無(wú)需給其他項(xiàng)提供名稱。
解構(gòu)賦值
let colors = ["red","green","blue"], firstColor = "black", secondColor = "purple"; [firstColor,secondColor] = colors; console.log(firstColor);//red console.log(secondColor);//green
數(shù)組解構(gòu)有一個(gè)非常獨(dú)特的用例,能輕易的互換兩個(gè)變量的值。
let a =1,b =2; [a,b] = [b,a]; console.log(a);//2 console.log(b);//1
嵌套的解構(gòu)
let colors = ["red", ["green", "blue"], "yellow"]; let [firstC, [, ssc]] = colors; console.log(ssc);//blue
剩余項(xiàng)
let colors = ["red", "green", "blue"]; let [firstC, ...restC] = colors; console.log(firstC); console.log(...restC); console.log(restC[0]);//green console.log(restC[1]);//blue
使用剩余項(xiàng)可以進(jìn)行數(shù)組克隆
let colors = ["red", "green", "blue"]; let [...restC] = colors; console.log(restC);//["red", "green","blue"]
三 混合解構(gòu)
let node = { type: "Identifier", name: 'foo', loc: { start: { line: 1, column: 1 }, end: { line: 1, column: 4 } }, range: [0, 3] } let { type, name: localName, loc: { start: { line: ll }, end: { column: col } }, range: [, second] } = node; console.log(type);//Identifier console.log(localName);//foo console.log(ll);//1 console.log(col);//4 console.log(second);//3
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
通過JS來(lái)判斷頁(yè)面控件是否獲取焦點(diǎn)
本篇文章主要介紹了通過JS來(lái)判斷頁(yè)面控件是否獲取焦點(diǎn)的方法。需要的朋友可以過來(lái)參考下,希望對(duì)大家有所幫助2014-01-01使用Promise封裝小程序wx.request的實(shí)現(xiàn)方法
這篇文章主要介紹了使用Promise封裝小程序wx.request的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11js實(shí)現(xiàn)鼠標(biāo)單擊Tab表單切換效果
這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)鼠標(biāo)單擊Tab表單切換效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05Three.js中網(wǎng)格對(duì)象MESH的屬性與方法詳解
三維開發(fā)渲染最多的對(duì)象大概是網(wǎng)格mesh了,Webgl開發(fā)三維也不例外,下面這篇文章主要給大家介紹了關(guān)于Three.js中網(wǎng)格對(duì)象MESH的屬性與方法,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-09-09js下劃線和駝峰互相轉(zhuǎn)換的實(shí)現(xiàn)(多種方法)
本文主要介紹了js下劃線和駝峰互相轉(zhuǎn)換的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10javascript實(shí)時(shí)獲取鼠標(biāo)坐標(biāo)值并顯示的方法
這篇文章主要介紹了javascript實(shí)時(shí)獲取鼠標(biāo)坐標(biāo)值并顯示的方法,涉及javascript操作鼠標(biāo)事件的相關(guān)技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04