I have a multiple instances of the classes that have the same parent class. And for specific case it is required to update parent class property so all available instances of the child classes would access updated property.
I can reach it with prototype in case if class property doesn't exist already:
class Foo {
someVal = 'Hello'
}
class Bar extends Foo {
}
let inst = new Bar()
Foo.prototype.someNewValue = 'Hello World'
console.log(inst.someNewValue) //Hello World
But in my case I need to update an already existing property:
class Foo {
someVal = 'Hello'
}
class Bar extends Foo {
}
let inst = new Bar()
Foo.prototype.someVal= 'Hello World'
console.log(inst.someVal) //Still output "Hello" because existing property value has a more priority than a value from prototype. While "Hello World" is desirable
With the prototype, you can not overwrite and change the property of class, but you can set the new property to the class
try this:
class Foo {}
class Bar extends Foo {}
let inst = new Bar();
Foo.prototype.someVal = "Hello World";
console.log(inst.someVal);
//hello world