js对象结构赋值const {XXX } =this

样例1:

const { xxx } = this.state;

上面的写法是es6的写法,其实就相当于:

const xxx = this.state.xxx

样例2:

const {comment,index,deleteComment} = this
上面的这句话是一个简写,最终的含义相当于
const  comment = this.comment
const  index = this.index
const   deleteComment = this.deleteComment

将一个对象展开并且将其中的部分属性给另一个对象

示例:

原对象 a={ a1=xxx, b1=xxx, c1=xxx } 想得到的b对象 b={ a1=xxx, b1=xxx }

方法:

const {a1,b1} = a; const b ={a1,b1}

// 把a对象里的a1,b1解构赋值

原理:

ES6解构赋值

对象的解构赋值是根据key值进行匹配

// 这里可以看出,左侧的name和右侧的name,是互相匹配的key值

// 而左侧的name匹配完成后,再赋值给真正需要赋值的Name

let { name:Name,age } = { name:'swr',age:28 }

console.log(Name) // 'swr'

console.log(age) // 28

posted @ 2021-10-13 18:11  喵师傅  阅读(3150)  评论(1编辑  收藏  举报