The only way that I can think of to alter the instance variables without making any changes to MyClass
would be to iterate over arr
.
Alternatively, if you really mean for all instances to share a common property, maybe what you want to do is use something similar to a static variable in classical programming. These can be accomplished by taking advantage of the fact that functions are really just objects in JavaScript, so you can add a property to the function constructor: MyClass.x = 1;
. When you want to change all instances' x
value, you can update them at once just by changing MyClass.x = 2;
. This technique alters the way you access the variable, so instead of this.x
you'll need to reference it by MyClass.x
.