在Arrow Functions旁边,这是我天天使用最多的ES6功能。ES6 Destructuring不是一个新功能,而是一种新的赋值语法,它容许您快速从对象属性和数组中解压缩值并将它们分配给单个变量。es6
var profile = {name:'George', age:39, hobby:'Tennis'}<font></font>json
var {name, hobby} = profile // destructure profile object<font></font>数组
console.log(name) // "George"<font></font>url
console.log(hobby) // "Tennis"对象
这里我用解构快速提取name和 hobby该属性profile的对象。ip
使用别名,您能够使用不一样的变量名称与相应的对象属性相比,您从如下位置提取值:it
var profile = {name:'George', age:39, hobby:'Tennis'}<font></font>io
var {name:n, hobby:h} = profile // destructure profile object<font></font>console
console.log(n) // "George"<font></font>变量
console.log(h) // "Tennis"
嵌套对象解构
解构也适用于嵌套对象,我老是使用它来快速解决来自复杂JSON请求的值:
var jsondata = {<font></font>
title: 'Top 5 JavaScript ES6 Features',<font></font>
Details: {<font></font>
date: {<font></font>
created: '2017/09/19',<font></font>
modified: '2017/09/20',<font></font>
},<font></font>
Category: 'JavaScript',<font></font>
},<font></font>
url: '/top-5-es6-features/'<font></font>
};<font></font>
<font></font>
var {title, Details: {date: {created, modified}}} = jsondata<font></font>
console.log(title) // 'Top 5 JavaScript ES6 Features'<font></font>
console.log(created) // '2017/09/19'<font></font>
console.log(modified) // '2017/09/20'