Destructuring assignment

2019-05-14 12:06:15 浏览数 (1)

Array destructuring

var [a,b] = [1,2]

var [a,b] = c //c=[1,2]

Object destructuring

var {a,b} = {a : 1, b : 2}

({a,b} = {a : 1, b : 2})

var {a,b} = c //c={a : 1 , b : 2}

({a,b} = c)

also provide support for default values, assigning to new variable names

关于Object destructing

Object的key用于匹配,左边的value应该是一个变量,右边的value应该有一个值。var {c} = {c:1}实际上是var {c:c} = {c:1},key和value相同的时候可以简写是es6的语法糖。

0 人点赞