You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
The text was updated successfully, but these errors were encountered:
Take the following example:
Expected output:
Actual output:
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:
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 versionsThe text was updated successfully, but these errors were encountered: