// 解构赋值
// 1.数组
var a, b, c
[a, b, c] = [1, 2, 3, 4, 5, 6, 7, 8, 9]
console.log(a, b, c);// 1 , 2 , 3
[d, e, ...f] = [1, 2, 3, 4, 5, 6, 7, 8, 9]
console.log(d, e, f); // 1 2 3-9
// 函数返回数组结构
function arrreturn() {
return [10, 20, 30, 40]
}
[arritem1, arritem2, arritem3, arritem4] = arrreturn()
console.log(arritem1, arritem2, arritem3, arritem4); //10 20 30 40
// 解构赋值数组的默认值
[defa1 = 1, defa2 = 2, defa3 = 3] = [10]
console.log(defa1, defa2, defa3); //10 2 3 可以使用等于号进行设置默认值,如果有解构赋的值,那么默认值不生效
// 2.对象
({ name, age } = { name: '张三', age: 25 })
console.log(name, age); //张三 25
({ name, age, ...surplus } = { age: 25, name: '李四', color: 'yellow', fruits: 'apple' })
console.log(name, age, surplus);//李四 25 {color: 'yellow', fruits: 'apple'}
var obj = { obja: 1, objb: 2 }
var { obja: objaContent, objb: objbContent } = obj
console.log(objaContent, objbContent); //1 2
// for of 迭代解构
var people = [
{
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:{mother:m,father:f}} of people){
console.log('my:'+n,'mother:'+m,'father:'+f);
}
ES6结构赋值
最新推荐文章于 2024-06-22 23:33:40 发布
853

被折叠的 条评论
为什么被折叠?



