Search code examples
javascriptjqueryobjectmergeextend

how does jquery extend() with multiple objects work?


I am trying to modify a plugin and am pulling my hair out trying to understand how extend() is working...

If I have this:

$.extend( plugin.ext.oStdClasses, {
      "some":"one"
       },
$.extend( plugin.ext.oStdClasses, plugin.ext.xtraClasses, {
      "some":"two"
       }

can someone explain to me what is happening?

Also I want to extend this with my own logic like so:

$.extend( plugin.ext.xtraClasses, plugin.ext.moreClasses, {
      "some":"thing",
      "else":"too"
       }

However this seems to give me back an empty object. Is it possible to extend the standard classes with the xtra classes with the more classes and overwrite the previous classes?

Thanks for some pointers


Solution

  • If you refer to the docs it states that the first argument for extend will be the object modified, so you're best doing something like this:

    var opts = {};
    $.extend(opts, plugin.ext.xtraClasses, plugin.ext.moreClasses, { 
       some: "thing",
       else: "too"
    });
    console.log(opts);