In JavaScript classes, the super keyword is used to call functions or methods defined in the parent class (also known as the superclass or base class). Its primary purpose is to access and execute these parent class functions within the context of the current subclass. This allows you to extend the behavior of the parent class in your subclass while still being able to use the functionality provided by the parent.
Here's how you can use the super keyword in JavaScript classes:
Constructor: In a subclass constructor, you can use super() to call the constructor of the parent class. This ensures that the initialization code in the parent class is executed before the subclass's own initialization.
class Parent {
constructor(name) {
this.name = name;
}
}
class Child extends Parent {
constructor(name, age) {
super(name); // Call the parent class constructor
this.age = age;
}
}
const child = new Child("Alice", 25);
console.log(child.name); // Outputs: "Alice"
console.log(child.age); // Outputs: 25
Method Calls: You can use super.methodName() to call a method defined in the parent class from the subclass. This allows you to override methods in the parent class while still having access to the original method.
class Parent {
greet() {
return "Hello from Parent!";
}
}
class Child extends Parent {
greet() {
return `${super.greet()} And Child!`;
}
}
const child = new Child();
console.log(child.greet()); // Outputs: "Hello from Parent! And Child!"
In summary, the super keyword is a crucial tool for working with inheritance in JavaScript classes. It helps maintain the connection between parent and subclass, allowing you to build on and customize the behavior of the parent class as needed.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
In JavaScript classes, the super keyword is used to call functions or methods defined in the parent class (also known as the superclass or base class). Its primary purpose is to access and execute these parent class functions within the context of the current subclass. This allows you to extend the behavior of the parent class in your subclass while still being able to use the functionality provided by the parent.
Here's how you can use the super keyword in JavaScript classes:
Constructor: In a subclass constructor, you can use super() to call the constructor of the parent class. This ensures that the initialization code in the parent class is executed before the subclass's own initialization.
Method Calls: You can use super.methodName() to call a method defined in the parent class from the subclass. This allows you to override methods in the parent class while still having access to the original method.
In summary, the super keyword is a crucial tool for working with inheritance in JavaScript classes. It helps maintain the connection between parent and subclass, allowing you to build on and customize the behavior of the parent class as needed.