首頁 > web前端 > js教程 > 主體

深入理解ES6資料解構

小云云
發布: 2018-01-15 09:08:20
原創
1243 人瀏覽過

相信大家都聽過ES6資料解構,本文主要幫助大家深入理解ES6之數據解構的用法,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟著小編過來看看吧,希望能幫助大家。

一物件解構

物件解構語法在賦值語句的左邊使用了物件字面量


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時,會拋出錯誤。

預設值

當你使用解構賦值語句時,如果所指定的本地變數在物件中沒有找到同名屬性,那麼變數會被賦值為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
登入後複製

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


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上。此語法與傳統物件字面量的語法相反

嵌套的物件結構


#
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//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
登入後複製

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
登入後複製

嵌套的解構


#

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
登入後複製
相關推薦:


js匯出Excel表格超出26位元英文字元的解決方法ES6

利用babel將es6語法轉es5的簡單方法

#ES6中類別的靜態方法有哪些作用

以上是深入理解ES6資料解構的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!