0

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 :|

Alex
  • 66,732
  • 177
  • 439
  • 641
  • From the code in your question, it looks like you're extending `function_arguments` with `defaults_arguments`, not the other way around. Are you sure you're not aiming for `$.extend({}, defaults_arguments, function_arguments)`? – Frédéric Hamidi Sep 28 '12 at 14:44
  • This link has some source and explanation of mechanics: http://www.blog.highub.com/javascript/decoding-jquery-objects-inherit-from-objects/ – asawyer Sep 28 '12 at 14:47
  • @FrédéricHamidi why is the first `{}` necessary? – Explosion Pills Sep 28 '12 at 14:49
  • @ExplosionPills, to merge `defaults_arguments` then `function_arguments` into a new object. Otherwise, `defaults_arguments` itself will be modified, which does not seem to be the questioner's intent. – Frédéric Hamidi Sep 28 '12 at 14:52
  • @FrédéricHamidi I understand; but then wouldn't you lose that complete? Like shouldn't you have to do `new_object = {}` first? – Explosion Pills Sep 28 '12 at 14:59
  • @ExplosionPills `var options = $.extend({}, defaults_arguments, function_arguments);` Or declare that new_object first too. `=]` – Fabrício Matté Sep 28 '12 at 15:00
  • @ExplosionPills, not really, because `$.extends()` returns the object it extends. In my example, it would take the new object `{}`, extend it with `defaults_arguments`, then with `function_arguments`, then return it to the caller. – Frédéric Hamidi Sep 28 '12 at 15:01
  • `jQuery.extend` will modify the first argument **and** return it, so there's no difference between `var options = {}; $.extend(options, defaults_arguments, function_arguments);` or `var options = $.extend({}, defaults_arguments, function_arguments);` – Fabrício Matté Sep 28 '12 at 15:03
  • ok, I had to assign the return value of $.extend to a variable. Then it works – Alex Sep 30 '12 at 17:33

3 Answers3

2

Your .extend is backwards. The defaults go first.

Explosion Pills
  • 188,624
  • 52
  • 326
  • 405
1

(I stand corrected, that's not how it works). Properties are only overwritten if a subsequent specified object carries that same property. The function is also not recursive (so the properties of an object property of an object will not be enumerated and merged) You may combine as many objects properties into the first object as you wish. Those multiple third, fourth, etc. arguments are merely the other objects you want incorporated into the first.

Try this:

$.extend(args, foo:this, bar:123);
L0j1k
  • 12,255
  • 7
  • 53
  • 65
0

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

Anton Baksheiev
  • 2,211
  • 2
  • 14
  • 15