樣例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