Search code examples
jqueryextend

How does $.extend work?


If I try it like

$.extend(function_arguments, defaults_arguments)

not all of the default_arguments get overwritten. For example if one of them is a function (this), it will remain in the object if the one from the function_arguments is of different type...

What does $.extend do exactly? Shouldn't it work like array_merge, but for objects?


ok, to be more clear:

....function(args){


  $.extend(args, {
     // here are defaults
     foo: this,
     abc: 1
  });

}

for some reason i get the default value for "foo", even if I have it inside args...

If I reverse the objects, like:

....function(args){


  $.extend({
     // here are defaults
     foo: this,
     abc: 1
  }, args);

}

I get only the properties from args, the defaults are missing completely :|


Solution

  • There's a perfect example on the jQuery Documentation : jQuery.extend