Normally when you extend it options with defaultOptions you create a new object, but I would like to re-use the options object. See below code.
I basically want to do the following:
var defaultOptions = {'width': '10px', 'size': '10px'};
var options = {'width': '300px', 'name': 'sam'};
// I want to re-use the same options variable as target for extend
var optionsAfterExtend = $.extend(options, defaultOptions, options);
// should return true
alert(options.width == '300px');
alert(optionsAfterExtend == '300px');
alert(options.size == '10px');
alert(optionsAfterExtend == options);
// Because I may add extra info to the options object which the client using my plugin can use / re-use
optionsAfterExtend.data = 'sam';
// So now optionsAfterExtend.data and options.data should both equal 'sam'
Tried searching for a long time and read the jquery extend documentation, but couldn't find a proper way to do it. Is it just stupid that I want this? Even if so I'm still curious how to do it in a nice way.
optionsAfterExtend.data = 'sam';there is no way thatoptionsAfterExtend.data == optionsAfterExtendwill return true, because your checking if the value of a property is the same as the object itself.optionsevery timeoptionsAfterExtendis changed?