国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > JS > 正文

深入理解ES6之數據解構的用法

2024-05-06 16:41:58
字體:
來源:轉載
供稿:網友

一 對象解構

對象解構語法在賦值語句的左側使用了對象字面量

let node = {  type: true,  name: false}//既聲明又賦值let {  type,  name} = node;//或者先聲明再賦值let type, name({type,name} = node);console.log(type);//trueconsole.log(name);//false

type與name標識符既聲明了本地變量,也讀取了對象的相應屬性值。

解構賦值表達式的值為表達式右側的值。當解構表達式的右側的計算結果為null或者undefined時,會拋出錯誤。

默認值

當你使用解構賦值語句時,如果所指定的本地變量在對象中沒有找到同名屬性,那么該變量會被賦值為undefined

let node = {  type: true,  name: false},  type, name, value;({type,value,name} = node);console.log(type);//trueconsole.log(name);//falseconsole.log(value);//undefined

你可以選擇性地定義一個默認值,以便在指定屬性不存在時使用該值。

let node = {    type: true,    name: false  },  type, name, value;({  type,  value = true,  name} = node);console.log(type);//trueconsole.log(name);//falseconsole.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這種語法表示要讀取名為type的屬性,并把它的值存儲在變量localType上。該語法與傳統對象字面量的語法相反

嵌套的對象結構

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}

當冒號右側存在花括號時,表示目標被嵌套在對象的更深一層中(loc: {start: localS,end: localE})

二 數據解構

數組解構的語法看起來跟對象解構非常相似,只是將對象字面量換成了數組字面量。

let colors = ["red", "blue", "green"];let [firstC, secondC, thirdC, thursC = "yellow"] = colors;console.log(firstC//redconsole.log(secondC);//blueconsole.log(thirdC);//greenconsole.log(thursC);//yellow

你也可以在解構模式中忽略一些項,并只給感興趣的項提供變量名。

let colors = ["red","green","blue"];let [,,thirdC] = colors;console.log(thirdC);//blue

thirdC之前的逗號是為數組前面的項提供的占位符。使用這種方法,你就可以輕易從數組任意位置取出值,而無需給其他項提供名稱。

解構賦值

let colors = ["red","green","blue"],  firstColor = "black",  secondColor = "purple";[firstColor,secondColor] = colors;console.log(firstColor);//redconsole.log(secondColor);//green

數組解構有一個非常獨特的用例,能輕易的互換兩個變量的值。

let a =1,b =2;[a,b] = [b,a];console.log(a);//2console.log(b);//1

嵌套的解構

let colors = ["red", ["green", "blue"], "yellow"];let [firstC, [, ssc]] = colors;console.log(ssc);//blue

剩余項

let colors = ["red", "green", "blue"];let [firstC, ...restC] = colors;console.log(firstC);console.log(...restC);console.log(restC[0]);//greenconsole.log(restC[1]);//blue

使用剩余項可以進行數組克隆

let colors = ["red", "green", "blue"];let [...restC] = colors;console.log(restC);//["red", "green","blue"]

三 混合解構

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);//Identifierconsole.log(localName);//fooconsole.log(ll);//1console.log(col);//4console.log(second);//3

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VeVb武林網。


注:相關教程知識閱讀請移步到JavaScript/Ajax教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 油尖旺区| 泉州市| 休宁县| 定南县| 太白县| 徐水县| 隆安县| 凉城县| 定安县| 乌拉特后旗| 乌海市| 石首市| 隆昌县| 德江县| 八宿县| 永宁县| 靖远县| 泰州市| 岱山县| 玉林市| 怀安县| 镇雄县| 四会市| 武清区| 太仆寺旗| 浪卡子县| 辛集市| 白河县| 平塘县| 岳阳县| 德庆县| 临安市| 肥乡县| 正镶白旗| 宜川县| 庐江县| 芷江| 泰州市| 金门县| 当阳市| 新乡市|