1

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

2 Answers 2

2

$.extend extending first passed argumnet.

for example:

var first = {first: true};
var second = {second: true}

$.extend(first, second);

console.log(first); // output {first: true, second: true}
console.log(second); // output {second: true}

var third = $.extend({}, first, second, {third: true});
console.log(third); // output {first: true, second: true, third: true}
Sign up to request clarification or add additional context in comments.

3 Comments

what happens if I omit the {} in var third?
the same as in the first case(first object extended too)
that's where my pulled out here is... ;-)
1

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);

1 Comment

so opts would be my basic classes to which xtra and more will be added?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.