对象的结构赋值

var obj = {
  name:"Jack",
  sex:"男",
 age:20,
  wife:{
    name:"Rose",
    sex:"女",
   age:18,
  },
}
var {name:a,sex,wife:{name:c,sex}} = obj;
console.log(a) //jack
console.log(c) //rose
console.log(name) //rose
console.log(sex) //女
console.log(x) //20
 
var {sex}=obj;  //只定义一个时,优先打印最外面的属性值
console.log(sex); //男

嵌套对象的解构赋值,

当有别名时,直接打印别名;

属性名相同时,优先打印最里面{ }内的属性.

posted @ 2019-08-23 22:33  无人问及  阅读(308)  评论(0编辑  收藏  举报