object解構賦值

let options = {
  title: 'menu',
  width: 100,
  height: 200
}
//若是簡寫,變量名必須和屬性名一致
let {title,width,height} = options
console.log(title,width,height)
let {title:title2,width,height} = options
console.log(title2,width,height)

let {title, ...last} = options
console.log(title,last)

//嵌套對象
let options = {
  size: {
    width:100,
    height:200
  },
  items:['he','ee'],
  extra: true
}
let {size:{width,height},items:[item1],extra} = options
console.log(width,height,item1,extra)
相關文章
相關標籤/搜索