es6--解构赋值-对象

 1:对象解构赋值的实质:找到对应的属性名,将值对应的赋给他
  {a,b}  等同于 {a:a,b:b}
   let {a:c,b} = {a:1,b:2}
  console.log(c, b) 
  2.对象的嵌套赋值
 let obj = {
p:[
'hello',
{y:'world'}
]
}
let {p,p:[x,{y:z}]} = obj
console.log(p,x,z) 
// 3:对象的解构可以有默认值,默认值生效的条件是严格等于undefined
/* let {x:t=2} = {x:4}
console.log(t) */
// 4:解构模式是嵌套的对象,而且子对象所在的父对象不存在,报错
/* let {foo:{bar:baz}} = {foo:{bar:'baz'}}
console.log(baz) */
// 5:将一个已经申明的变量用于解构赋值
/* let x;
({x} = {x:1})
console.log(x) */
/* let {sin} = Math
console.log(sin(60)) */
// 6:对数组进行对象属性的解构
/* let arr = [1,2];
let {0:first,1:last} = arr
let {0:first,[arr.length-1]:last} = arr
// 注意此处arr.length-1用的是[]
console.log(first,last) */
// 7:字符串的解构赋值
/* let [a,b,c] = 'hel'
console.log(a,b,c)
let {length} = 'hel'
console.log(length) */
posted @ 2017-12-16 12:03  王玮-web  阅读(367)  评论(0编辑  收藏  举报