typescript parent class is calling derived function
inheritance, oop, typescript
Solution
In case, we want base `init()` to be called and then child `init()` we can solve it like this:
constructor() {
super.init();
super();
}
Check the example here
Solution when firstly child and then base should be like this.
constructor() {
super();
//this.init();
super.init();
}
A working example is in this playground. Click the RUN to see two buttons generated
If we want just our derived stuff to be used, we do not have to call `init` again:
constructor() {
super();
//this.init();
// init will be called in super
}
This example just uses the child stuff
Problem
I have got a base class and a derived one, each has init function. When i construct the derived one i want it to: call its base constructor which: 1.1. calls its init function call its own(derived) init function. The problem is that the derived init function is called twice. Code: ``` class Base{ constructor() { this.init(); } init(){ console.log("Base init"); } } class Derived extends Base{ constructor() { super(); this.init(); } init(){ console.log("Derived init"); } } var obj = new Derived (); ``` Output: ``` Derived init Derived init ```