重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

avaScriptES6值得掌握的五大功能(4)JavaScript解构

在Arrow Functions旁边,这是我每天使用最多的ES6功能。ES6 Destructuring不是一个新功能,而是一种新的赋值语法,它允许您快速从对象属性和数组中解压缩值并将它们分配给单个变量。

成都创新互联公司于2013年成立,是专业互联网技术服务公司,拥有项目网站设计、成都网站设计网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元麻城做网站,已为上家服务,为麻城各地企业和个人服务,联系电话:028-86922220

1

2

3

4

var profile = {name: 'George' , age:39, hobby: 'Tennis' }

var {name, hobby} = profile // destructure profile object

console.log(name) // "George"

console.log(hobby) // "Tennis"

这里我用解构快速提取 name 和  hobby 该属性 profile 的对象。

使用别名,您可以使用不同的变量名称与相应的对象属性相比,您从以下位置提取值:

1

2

3

4

var profile = {name: 'George' , age:39, hobby: 'Tennis' }

var {name:n, hobby:h} = profile // destructure profile object

console.log(n) // "George"

console.log(h) // "Tennis"

嵌套对象解构

解构也适用于嵌套对象,我总是使用它来快速解决来自复杂JSON请求的值:

1

2

3

4

6

7

8

9

10

11

12

13

14

15

16

var jsondata = {

     title: 'Top 5 JavaScript ES6 Features' ,

     Details: {

         date: {

             created: '2017/09/19' ,

             modified: '2017/09/20' ,

         },

         Category: 'JavaScript' ,

     },

     url: '/top-5-es6-features/'

};

var {title, Details: {date: {created, modified}}} = jsondata

console.log(title) // 'Top 5 JavaScript ES6 Features'

console.log(created) // '2017/09/19'

console.log(modified) // '2017/09/20'


文章名称:avaScriptES6值得掌握的五大功能(4)JavaScript解构
网站地址:http://cqcxhl.com/article/gighjg.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP