Sharing the progress of providing answers and solutions to OP, who had not shared all necessary information upfront. Despite offering a solution that was later marked correct by someone else, it was initially dismissed.
Initially suggested a straightforward solution:
Moved code outside constructor function to update shared property for all instances on window resize.
window.addEventListener('resize', function() {
Me.prototype.window = window.outerWidth;
};
Later, OP noted that registering callback on prototype could cause issues if no instances of Me
existed.
Suggested tracking instance existence before registering event listener, which OP eventually adopted.
Introduced global variable to track instance presence:
// Global variabel to track if Me instances exist
var meInstances = false
var me1 = new Me();
meInstances = true;
if(meInstances){
window.addEventListener('resize', function() {
Me.prototype.window = window.outerWidth;
};
}
Received feedback critiquing this approach as adding unnecessary complexity and lack of robustness. OP then devised an array-based solution to address these concerns.
Proposed another alternative utilizing callbacks within constructor function, albeit not fully endorsing it due to evolving constraints set by OP.
function Me() {
// This code only runs when instance is being made
// Instance registers a callback
window.addEventListener('resize', function() {
// Callback modifies single Prototype that all instances share
Me.prototype.window = window.outerWidth;
});
}
Me.prototype.window = {};
Despite various suggestions and adaptations, none were ultimately accepted as a suitable solution.