Lets say I have an object constructor that a third-party page developer can insert in their page, called “Widget”. It could be something like a tooltip, for example.
var Widget = function(settings, callbacks) {
this.hide = function() {
//some code...
};
this.show = function() {
this.isShowing = true;
//more code...
}
}
When this Widget is shown, I want to hide any other shown Widget. Is there a way the “show” method can call the “hide” method on the other instance(s) of Widget whose “this.isShowing” is true, or on all instances of Widget?
You can, but you need to maintain references to those instances. You could maintain them on a property of the function itself:
And then, just access
Widget.instancesto get the all the widgets created.You may want to create some kind of destructor to remove a widget from the list though (
myWidget.destroy()or something), because this might generate a memory leak if you instantiate too many of them.