Reputation: 776
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?
Upvotes: 0
Views: 109
Reputation: 29971
You can, but you need to maintain references to those instances. You could maintain them on a property of the function itself:
var Widget = function(settings, callbacks) {
Widget.instances.push(this);
// More code
};
Widget.instances = [];
And then, just access Widget.instances
to 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.
Upvotes: 3