您的位置:首页 > 其它

Web端 es6(基础二) 解构赋值

2018-02-02 18:49 447 查看

数组解构

{
let a,b,rest;
[a,b,...rest] = [1,2,3,4,5,6,7]
console.log(a,b,rest)
}

{
let a,b;
({a,b} = {a:1,b:2})
console.log(a,b)
}

{
let a,b,c;
[a,b,c = 3] = [1,2];
console.log(a,b,c)
}
输出结果
1 2 (5) [3, 4, 5, 6, 7]
1 2
1 2 3


实战用途

{
// 交换变量
let a=1;
let b=2;
[a,b]=[b,a]
console.log(a,b)
}

{
function f(){
return [1,2]
}
let a,b;
[a,b]=f()
console.log(a,b)
}

{
function f(){
return [1,2,3,4,5]
}
let a,b,c;
[a,,,b]=f()
console.log(a,b)
}

// 输出结果
2 1
1 2
1 4


实例分析

let metaData  = {
title: 'abc',
test: [{
title: '中国',
desc: '啦啦啦啦啦'
}]
}
let {title:esTitle,test:[{title:cnTitle}]} = metaData;
console.log(esTitle,cnTitle)
// 输出结果
abc 中国
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  es6 web