When creating a parent class type, I want to be able to have a number of child types. The parent is constantly remaking itself, and I want the children to be able to remake themselves, instead of being remade as the parent.
Here is an overly simplified example.
class Vector {
constructor(...c) { this.components = c; }
add(c) { return new Vector([this.components[0]+c]) }
}
class Vec2 extends Vector {}
In this example, If I make a new Vec2, and use its add method, it will return a new Vector, but I want it to return a new Vec2. How is this done in Javascript?
In other words, I want something like this.
class Vector {
constructor(...c) { this.components = c; }
add(c) { return new CurrentClassExtension([this.components[0]+c]) }
// ^^^ How to access the Vector or Vec2 or whatever Class extension here
}
class Vec2 extends Vector {}