I have one class that extends another, in the inherited class constructor I am overwriting a property also used in the parent's constructor.
class Animal {
name = "animal";
constructor() {
console.log(this.name);
}
}
class Rabbit extends Animal {
constructor() {
super();
this.name = 'rabbit';
}
}
new Animal(); // shows animal
new Rabbit(); // shows animal
Since at the time the parent's constructor is called the new value is not yet available, it shows "animal" again. I know this is due to the JS internals.
The page where I found the problem says that it can be fixed by using methods or getters/setters instead of fields, but I can't find a way to work around it. How can I make it show the new value?