I have an issue with the data within an extension class does not change when I change data within the super class.
This might be an XY issue or caused by the fact that I’m still not used to using class. I already read into mixin, but I don’t see yet how it would help me.
As an example:
I have a class “container” with the methods: height, depth, width, and volume. The defaults are set within the constructor by a settings file. height, width, and depth can be changed through setters, while the volume is always calculated. A container always exists; it can also exist without a product.
I also have a class with products that have a quantity and density. There is actually more to it, but to simplify it, I am limiting it to this in the example.
Basically, I call the extension class product that can only exist within a container. it calculates the quantity based on the densityand the volume of the super class.
That is the part that works fine.
The issue now is that the container size might change, and therefore also the volume. Which means the quantity of the product should change too, but it doesn’t. The extended class does not recognize an update of the super class data.
How can I make it work?
class Container {
#height;
#depth;
#width;
constructor() {
this.#height = 10;
this.#depth = 10;
this.#width = 10;
}
set width(length) {
this.#width = length;
}
get volume() {
return this.#width * this.#height * this.#depth;
}
}
class Product extends Container {
#density;
constructor(volume) {
super(volume);
this.#density = 4;
}
get quantity() {
return this.volume * this.#density;
}
}
const container = new Container();
const product = new Product();
// expectin 4,000 - true
console.log(product.quantity);
// expecting 6,000 - false
container.width = 15;
console.log(product.quantity);
