Within my hierarchy, there is some static content present in all levels (for example, the _image
). It would be ideal to access the corresponding _image
without repeating code:
Here's what I envision:
class Actor {
static _image; // Needs to be static
method show(){ // Needs to be non-static
this.setImage(this.class._image); // Doesn't work....
}
}
class GoodActor extends Actor {
static _image = 'good.png'
}
class BadActor extends Actor {
static _image = 'bad.png'
}
class MediumActor extends Actor {
static _image = 'medium.png'
}
Unfortunately, it doesn't work. At the moment, I've only managed to get to:
class Actor {
}
class GoodActor extends Actor {
static _image = 'good.png' // Needs to be static
method show(){ // Needs to be non-static
this.setImage(GoodActor._image);
}
}
class BadActor extends Actor {
static _image = 'bad.png' // Needs to be static
method show(){ // Needs to be non-static
this.setImage(BadActor._image);
}
}
class MediumActor extends Actor {
static _image = 'medium.png' // Needs to be static
method show(){ // Needs to be non-static
this.setImage(MediumActor._image);
}
}
If these four classes have more methods, I don't want to repeat the show()
method in each subclass... However, I require the show()
method to be non-static and the _image
to be accessed statically.
I have come across this issue in TypeScript: https://github.com/Microsoft/TypeScript/issues/7673, but unfortunately, I can't seek help there as they have closed it without a solution. None of them addressed the challenge of dynamically resolving the static method for invocation.