最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

深入理解ES6之數(shù)據(jù)解構(gòu)的用法

 更新時間:2018年01月13日 09:05:06   作者:尋找石頭魚  
本文介紹了深入理解ES6之數(shù)據(jù)解構(gòu)的用法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

一 對象解構(gòu)

對象解構(gòu)語法在賦值語句的左側(cè)使用了對象字面量

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標識符既聲明了本地變量,也讀取了對象的相應(yīng)屬性值。

解構(gòu)賦值表達式的值為表達式右側(cè)的值。當解構(gòu)表達式的右側(cè)的計算結(jié)果為null或者undefined時,會拋出錯誤。

默認值

當你使用解構(gòu)賦值語句時,如果所指定的本地變量在對象中沒有找到同名屬性,那么該變量會被賦值為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上。該語法與傳統(tǒng)對象字面量的語法相反

嵌套的對象結(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}

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

二 數(shù)據(jù)解構(gòu)

數(shù)組解構(gòu)的語法看起來跟對象解構(gòu)非常相似,只是將對象字面量換成了數(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)模式中忽略一些項,并只給感興趣的項提供變量名。

let colors = ["red","green","blue"];

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

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

解構(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)有一個非常獨特的用例,能輕易的互換兩個變量的值。

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

剩余項

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

使用剩余項可以進行數(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

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

四会市| 恭城| 大姚县| 临邑县| 临江市| 福建省| 夏河县| 鲜城| 务川| 沿河| 乌鲁木齐市| 大姚县| 咸阳市| 巍山| 四子王旗| 长葛市| 建始县| 武城县| 新田县| 平利县| 瑞昌市| 拜泉县| 合阳县| 永寿县| 颍上县| 九寨沟县| 蚌埠市| 定远县| 朔州市| 弥渡县| 淄博市| 旅游| 铜鼓县| 堆龙德庆县| 安岳县| 星座| 禄劝| 宁津县| 镇康县| 五家渠市| 靖边县|