合并对象

1、Object.assign

function custion (options) {
  this.default = { 
     width : 100 ,
     height : 100 ,
     color : "red"
  }
  this.settings = Object.assign(this.default , options)
  console.log(this.settings.width)  
  //显示 {width: 200, height: 100, color: "red", background: "red"}
}
custion({
  width : 200 ,
  background: "red"
})

 

2、Jquery中 $.extend:

function custion (options) {
  this.default = { 
     width : 100 ,
     height : 100 ,
     color : "red"
  }
  this.settings = $.extend(this.default , options)
  console.log(this.settings.width)
//显示 {width: 200, height: 100, color: "red", background: "red"} }
custion({
  width : 200 ,
  background: "red"
})
posted @ 2019-12-15 21:02  雨夜稻草  阅读(133)  评论(0编辑  收藏  举报