1. 数组解构赋值
var arr = ['this is a string', 2, 3];
//传统方式
var a = arr[0],
b = arr[1],
c = arr[2];
//解构赋值,是不是简洁很多?
var [a, b, c] = arr;
console.log(a);//this is a string
console.log(b);//2
console.log(c);//3
//1.嵌套解构
var arr = [[1, 2, [3, 4]], 5, 6];
var [[d, e, [f, g]], h, i] = arr;
console.log(d);//1
console.log(f);//3
console.log(i);//6
//2.函数传参解构
var arr = ['this is a string', 2, 3];
function fn1([a, b, c]) {
console.log(a);
console.log(b);
console.log(c);
}
fn1(arr);
//this is a string
//2
//3
//3.for循环解构
var arr = [[11, 12], [21, 22], [31, 32]];
for (let [a, b] of arr) {
console.log(a);
console.log(b);
}
//11
//12
//21
//22
//31
//32
2.对象赋值解构
var obj = {
name: 'chris',
sex: 'male',
age: 26,
son: {
sonname: '大熊',
sonsex: 'male',
sonage: 1
}
};
var {name, sex, age, son} = obj;
console.log(name + ' ' + sex + ' ' + age); //chris male 26
console.log(son); // { sonname: '大熊', sonsex: 'male', sonage: 1 }
1.对象传参解构
var obj = {
name: 'chris',
sex: 'male',
age: 26,
son: {
sonname: '大熊',
sonsex: 'male',
sonage: 1
}
};
function fn2({sex, age, name}) {
console.log(name + ' ' + sex + ' ' + age);
}
fn2(obj);
//chris male 26
2.变量名与对象属性名不一致解构:
var obj = {
name: 'chris',
sex: 'male',
age: 26
};
var {name: nickname, age: howold} = obj;
console.log(nickname + ' ' + howold); //chris 26
3.嵌套对象解构:
var obj = {
name: 'chris',
sex: 'male',
age: 26,
son: {
sonname: '大熊',
sonsex: 'male',
sonage: 1
}
};
var {name, sex, age, son: {sonname, sonsex, sonage}} = obj;
console.log(sonname + ' ' + sonsex + ' ' + sonage);
//大熊 male 1
//Babel暂不支持这种嵌套解构
obj = {
name: 'chris',
sex: 'male',
age: [1, 2, 3]
}
{name, sex, age: [a, b, c]} = obj;
console.log(c);
4.套对象属性重名,解构时需要更改变量名
var obj = {
name: 'chris',
sex: 'male',
age: 26,
son: {
name: '大熊',
sex: 'male',
age: 1
}
};
//赋值解构
var {name: fathername, son: {name, sex, age}} = obj;
console.log(fathername); //chris
console.log(name); //大熊
//传参解构
function fn3({sex, age, name, son: {name: sonname}}) {
console.log(name + ' ' + sex + ' ' + age + ' ' + sonname);
}
fn3(obj);
//chris male 26 大熊
//5.循环解构对象
var arr = [{name: 'chris', age: 26}, {name: 'jack', age: 27}, {name: 'peter',age: 28}];
for (let {age, name} of arr) {
console.log(name + ' ' + age);
}
//chris 26
//jack 27
//peter 28
//6.解构的特殊应用场景
//变量互换
var x = 1,
y = 2;
var [x, y] = [y, x];
console.log(x); //2
console.log(y); //1
//字符串解构
var str = 'love';
var [a, b, c, d] = str;
console.log(a);//l
console.log(b);//o
console.log(c);//v
console.log(d);//e
摘自:妙用ES6解构和扩展运算符让你的代码更优雅
网友评论