Description
Take the following example:
$.widget('test.greet', {
options: {names:['anonymous']},
_create: function(){
this.options.names.unshift('Hello');
console.log(this.options.names.join(' '));
}
});
$('<div>').greet({names:['Jools']});
$('<div>').greet();
$('<div>').greet();
$('<div>').greet({names:['Adam']});
$('<div>').greet();
Expected output:
Hello Jools
Hello anonymous
Hello anonymous
Hello Adam
Hello anonymous
Actual output:
Hello Jools
Hello anonymous
Hello Hello anonymous
Hello Adam
Hello Hello Hello anonymous
Things work as expected so long as you don't rely on the default options array.
When the options
object is initialised, objects are recursively recreated to ensure that they're unique to an instance. Arrays are excluded from this, which seems to be an intentional choice to preserve performance:
#193
I understand not cloning provided arrays, as they can be massive, but surely we could at least clone the default/prototype arrays. Very often an empty array is specified as a fallback or even just to clearly show what type the option is.
Obviously using an array reference that was passed in is always potentially dangerous, as you're assuming the caller is done with it, but that is a much more obvious problem than the default array being shared between all instances. Presumably this could be addressed without any significant performance hit.
I've managed to workaround this in a fairly central place as I have a base widget where I can call the following. This just recursively looks for arrays from the prototype and rebuilds them:
_cloneOptionsArrays: function _cloneOptionsArrays(protoObj, optionObj) {
protoObj = protoObj || this.__proto__.options;
optionObj = optionObj || this.options;
let key;
for (key in optionObj) {
const value = optionObj[key];
const protoValue = protoObj[key];
if ($.isPlainObject(value) && protoValue) {
that._cloneOptionsArrays(protoValue, value);
} else if (Array.isArray(value) && protoValue === value) {
optionObj[key] = [].concat(value);
}
}
},
I've actually only tested this in jquery ui 1.12 (sorry), but the same code ($.widget.extend
) appears to be present in all later versions