Tutorial :In jQuery, using $.extend(), is this redundant?


Hey, I was wondering if setting an object equal to $.extend(), but setting the target to the same object is redundant?

 setOptions(options = $.extend({          classPrefix: 'imgareaselect',          movable: true,          resizable: true,          parent: 'body',          onInit: function () {},          onSelectStart: function () {},          onSelectChange: function () {},          onSelectEnd: function () {}      }, options));  


They way you've coded it, it's not redundant. That's because the first object passed in is the one that's modified, so all of the properties in options are being copied to the anonymous object you pass in as the first parameter. So you would then need to assign the return value of $.extend to something in order by be able to actually use it. If you'd done it the other way around:

$.extend(options, { ... });  

Then the assignment is redundant. Note also that the order you pass objects in is important. By doing it the way you've done it, the anonymous object is basically the "defaults" and anything in options will override them.

Finally, because you're passing the result to a call to setOptions (which I assume saves the value somewhere) you also don't need an explicit assignment to options. But that depends entirely on what setOptions does and what you need options for later...

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »