I have a situation like that:
class TestClass {
constructor() {
shared logic
}
anotherFunction() {
shared logic
}
}
How can I achieve that not duplicating the code?
As always, create a function for the shared logic, either inside of the class or outside of it.
class TestClass {
constructor() {
this.sharedLogicFunction();
}
anotherFunction() {
this.sharedLogicFunction();
}
sharedLogicFunction() {}
}
super must be called before this is referenced. If for some weird reason sharedLogicFunction needs to occur before a call to super, sharedLogicFunction would need to be made static or defined outside the class altogether.
anotherFunctionand call it from the constructor?