Skip to content
Advertisement

How can I avoid code repetition when overriding many properties in a child class in the same way?

I have a class in which I have properties that are returning arrays. For simplicity, let’s consider them constant:

JavaScript

Now, I have another class which is inheriting MyClass and it is interpolating the data, for example:

JavaScript

Now, the issue is that I have 2 classes like MyClass and each one of them consists of ~30 properties.

Is there a way to override all properties without doing it one by one? I was having a look also at this solution but I am not sure if/how I can apply it to my problem.

Advertisement

Answer

Refactor your superclass to “proxy”/”trampoline” those properties via a function you can override in a subclass:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement