class Person{
constructor(name){
this.name = name;
}
}
class Financial extends Person{
constructor(salary){
this.salary = salary;
}
}
// demand and debt class
class DD extends Financial{
constructor(amount){
this.amount = amount;
}
}
let jack = new Person('jack');
I have 3 classes which inherent each other.
I made an instance of first class(person),
now how can I make a new instance of DD class for jack?
I’m making a simple Accounting webApp and this is my problem.
2
Answers
I found the solution.
To create a new instance of the
DD
class for the existing instancejack
, you need to follow the inheritance chain and make sure that the constructors of each class are called properly. In your current implementation, the constructors of the child classes (Financial
andDD
) are not calling the constructors of their parent classes (Person
andFinancial
). To achieve this, you can use thesuper
keyword inside the child class constructors.Here’s the updated implementation of your classes:
In this example, we added
super(name, salary)
in the constructor ofFinancial
to call the constructor ofPerson
, passing thename
argument to it. Similarly, in the constructor ofDD
, we usedsuper(name, salary)
to call the constructor ofFinancial
, passing bothname
andsalary
arguments to it.Now, you can create a new instance of
DD
for the existing instancejack
by passing the required arguments (name, salary, and amount) to theDD
constructor as shown in thejackAccount
instantiation above. This way, you can maintain the inheritance chain and create instances for your classes accordingly in your Accounting webApp.