第五章、解构:使数据访问更便捷
1、对象解构赋值
1、默认值
let node = {
type : "js",
name: "lisi"
};
let { type, name, value = true } = node;
2、数组解构
数组解构,使用的是数组字面量。
let colors = ['red','yellow'];
let [firstColor,secondColor] = color;
console.log(firstColor); //'red'
console.log(secondColor); //'yellow'
一个特殊的使用场景:交换两个变量的值
let a = 1, b = 2;
[a,b] = [b,a];
console.log(a); //2
console.log(b); //1
支持默认值。
不定元素:
let colors = ['red','yellow','green'];
let [firstColor,...restColor] = color;
console.log(firstColor); //'red'
console.log(restColor.length); //2
实现concat()方法
let colors = ['red','yellow','green'];
let [...cloneClors] = colors;
console.log(cloneColors); //"['red','yellow','green']"
3、混合解构
4、结构参数
function setCookie(name,value,{path,secure,domine,expires}={}){
...
}