es6解构赋值
定义:
官方解释:解构赋值就是从目标对象或数组中提取自己想要的变量。最常用的场景是:element-ui,vant-ui按需引入,请求接口返回数据,提取想要的数据。 通俗解释:意思就是从目标对象或数组中提取出自己想要的变量(数据), 常用场景:element-ui,vant的按需引入,请求接口,返回数据时,提取自己想要的数据
语法:
如果传参时传了参数就使用传的参数,如果没有传送参数就使用默认值 解构赋值就是从对象或数组中提取单个变量或属性 数组解构时两边的结构类型必须相等; 对象结构时,只会对应属性名,没有顺序。也可以用...来解构。 一般应用于交换变量,传默认值,忽略某个值,element-ui的单个组件引入,for..of..循环等。
var a, b, rest;
[a, b] = [10, 20];
console.log(a); // 10
console.log(b); // 20
//对象解构
({ a, b } = { a: 10, b: 20 });
console.log(a); // 10
console.log(b); // 20
// ...rest 解构数组
[a, b, ...rest] = [10, 20, 30, 40, 50];
console.log(a); // 10
console.log(b); // 20
console.log(rest); // [30, 40, 50]
// ...rest 解构对象(最新)
({a, b, ...rest} = {a: 10, b: 20, c: 30, d: 40});
console.log(a); // 10
console.log(b); // 20
console.log(rest); // {c: 30, d: 40}
//解析一个从函数返回
function f() {
return [1, 2];
}
var a, b;
[a, b] = f();
console.log(a); // 1
console.log(b); // 2
For of 迭代和解构
{
name: 'Mike Smith',
family: {
mother: 'Jane Smith',
father: 'Harry Smith',
sister: 'Samantha Smith'
},
age: 35
},
{
name: 'Tom Jones',
family: {
mother: 'Norah Jones',
father: 'Richard Jones',
brother: 'Howard Jones'
},
age: 25
}
];
for (var {name: n, family: {f