合并对象

Posted yuyedaocao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了合并对象相关的知识,希望对你有一定的参考价值。

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"
})

以上是关于合并对象的主要内容,如果未能解决你的问题,请参考以下文章