es6为对象和数组添加了结构功能,将数据结构打散的过程便得更加简单,从打算后更小的部分中获取所需信息。
对象解构:
let node = { type :"idep", name:"foo" } let {type,name} = "node"; console.log(type);//idep console.log(name);//foo
使用解构赋值表达时,如果指定的局部变量名在对象中不存在,那么局部变量名会被赋值为undefined;
数组解构:
let color =["red","green","blue"]; let [firstColor,secondColor] = color; console.log(firstColor,secondCOlor)//red,green
es6交换变量:
let a = 1;
let b= 2;
[a,b]=[b,a]
console.log(a)//2
console.log(b)//1
不定元素:
let colors = ["red","green","blue"];
let [firstColor,...reColor]=colors;
console.log(firsetColor)//red
console.log(reColor[0])//green
console.log(reColor[1])//blue