深刻解析ES6之數據解構的用法

本文介紹了深刻理解ES6之數據解構的用法,寫的十分的全面細緻,具備必定的參考價值,對此有須要的朋友能夠參考學習下。若有不足之處,歡迎批評指正。前端

一 對象解構node

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

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標識符既聲明瞭本地變量,也讀取了對象的相應屬性值。 解構賦值表達式的值爲表達式右側的值。當解構表達式的右側的計算結果爲null或者undefined時,會拋出錯誤。學習

默認值3d

當你使用解構賦值語句時,若是所指定的本地變量在對象中沒有找到同名屬性,那麼該變量會被賦值爲undefinedrest

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
//歡迎加入前端全棧開發交流圈一塊兒吹水聊天學習交流:864305860

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

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這種語法表示要讀取名爲type的屬性,並把它的值存儲在變量localType上。該語法與傳統對象字面量的語法相反blog

嵌套的對象結構開發

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}
//歡迎加入前端全棧開發交流圈一塊兒吹水聊天學習交流:864305860

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

二 數據解構

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

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

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

let colors = ["red","green","blue"];
 
let [,,thirdC] = colors;
console.log(thirdC);//blue
//歡迎加入前端全棧開發交流圈一塊兒吹水聊天學習交流:864305860

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

解構賦值

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

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

let a =1,b =2;
[a,b] = [b,a];
console.log(a);//2
console.log(b);//1
//歡迎加入前端全棧開發交流圈一塊兒吹水聊天學習交流:864305860

嵌套的解構

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]);//green
console.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);//Identifier
console.log(localName);//foo
console.log(ll);//1
console.log(col);//4
console.log(second);//3
//歡迎加入前端全棧開發交流圈一塊兒吹水聊天學習交流:864305860

結語

感謝您的觀看,若有不足之處,歡迎批評指正。

相關文章
相關標籤/搜索