Calling Parent Constructors with super
When you create a subclass in JavaScript using the extends keyword, the subclass inherits properties and methods from its parent class. However, to properly initialize the parent class's properties within the subclass's constructor, you must use the super keyword. The super keyword acts as a reference to the parent class's constructor and allows you to call it from within the subclass. This is essential because, in JavaScript, a subclass constructor cannot access this until after it has called super. By calling super, you ensure that all properties defined in the parent class are correctly set up before you add or modify properties specific to the subclass. This mechanism not only enables property inheritance but also allows you to extend or customize the initialization process for your subclasses.
1234567891011121314151617181920// Parent class class Vehicle { constructor(make, model) { this.make = make; this.model = model; } } // Subclass class Car extends Vehicle { constructor(make, model, doors) { super(make, model); // Call parent constructor this.doors = doors; // Additional property for Car } } const myCar = new Car("Toyota", "Corolla", 4); console.log(myCar.make); // Output: Toyota console.log(myCar.model); // Output: Corolla console.log(myCar.doors); // Output: 4
Takk for tilbakemeldingene dine!
Spør AI
Spør AI
Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår
Can you explain what happens if you forget to call `super` in the subclass constructor?
How does method overriding work in JavaScript subclasses?
Can you show an example with more than one level of inheritance?
Awesome!
Completion rate improved to 6.25
Calling Parent Constructors with super
Sveip for å vise menyen
When you create a subclass in JavaScript using the extends keyword, the subclass inherits properties and methods from its parent class. However, to properly initialize the parent class's properties within the subclass's constructor, you must use the super keyword. The super keyword acts as a reference to the parent class's constructor and allows you to call it from within the subclass. This is essential because, in JavaScript, a subclass constructor cannot access this until after it has called super. By calling super, you ensure that all properties defined in the parent class are correctly set up before you add or modify properties specific to the subclass. This mechanism not only enables property inheritance but also allows you to extend or customize the initialization process for your subclasses.
1234567891011121314151617181920// Parent class class Vehicle { constructor(make, model) { this.make = make; this.model = model; } } // Subclass class Car extends Vehicle { constructor(make, model, doors) { super(make, model); // Call parent constructor this.doors = doors; // Additional property for Car } } const myCar = new Car("Toyota", "Corolla", 4); console.log(myCar.make); // Output: Toyota console.log(myCar.model); // Output: Corolla console.log(myCar.doors); // Output: 4
Takk for tilbakemeldingene dine!