1 数组解构赋值
{
// 数组
let a,b,c;
[a,b,c]=[1,2,3];
console.info(a,b,c)
}
输出:
{
let [a,...b]=[1,2,3,4,5,6];
console.info(a,b);
}
输出:
{
let a,b,c;
[a,b,c]=[1,2];
console.info(a,b,c);
[a,b,c=3]=[4,5];
console.info(a,b,c);
}
输出:
2 对象解构赋值
{
let a,b;
({a,b}={a:4,b:5})
console.log(a,b);
}
输出:
{
let o={p:42,q:true};
let {p,q}=o;
console.log(p,q);
}
输出:
3 解构应用 - 变量互换
{
let a=1;
let b=2;
console.info(a,b);
[a,b]=[b,a];
console.info(a,b);
}
输出:
4 解构应用 - 函数多个返回值
{
function test()
{
return [1,2,3];
}
let [a,,c]=test();
console.info(a,c)
//输出1 3
}
输出:
5 解构应用 - JSON赋值
{
// 前端返回的数据
let returnJson = {
title:'abc',
test:[{
title:'test',
desc:'description'
}]
};
let {title:esTitle,test:[{title:cnTitle}]}=returnJson;
console.info(esTitle,cnTitle);
}
输出: