I have something similar to the following code for a parent and child class:
const modify = (data) => {
const newData = data
// changes newData in some way
return newData
}
class Parent {
constructor(data) {
this.data = data
}
modify() {
return new Parent(modify(this.data))
}
}
class Child extends parent {
constructor(data) {
super(data)
}
}
Is there a way to change the Parent class’s modify
function to return an instance of Child when it is called using an instance of Child?
I know that in a static function, you can use new this()
to do that, is there something equivalent for methods? Or would I have to overwrite the Parent method in the Child class?