How can I merge 2 javascript objects, populating the properties in one if they don't exist in the other?

后端 未结 5 1341
太阳男子
太阳男子 2021-02-18 16:47

If I have a javascript object/assoc. array defined like this:

function somefunction(options) {

    var defaults = {
        prop1: \'foo\',
        prop2: \'bar         


        
5条回答
  •  遥遥无期
    2021-02-18 17:11

    Using Google Closure Library it can be done like that:

    goog.require('goog.object');
    function somefunction(options) {
      var defaults = {
        prop1: 'foo',
        prop2: 'bar'
      };
      goog.object.extend(defaults, options);
      // if the property is defined in options it will overwrite value.
    }
    

提交回复
热议问题