JavaScript Class super Keyword
- Previous Page static
- Next Page constructor()
- Go to Parent Layer JavaScript Class Reference Manual
Definition and Usage
super
Keyword refers to the parent class.
It is used to call the constructor of the parent class and access the properties and methods of the parent class.
Tips:To better understand the concept of "inheritance" (parent class and subclass), please read our JavaScript Class Tutorial.
Instance
Create a class named "Model", which will use extends
the method of inheriting "Car" class. extends
By calling in the constructor method super()
Methods, we will call the constructor of the parent class and can access the properties and methods of the parent class:
class Car { constructor(brand) { this.carname = brand; } present() { return 'I have a ' + this.carname; } } class Model extends Car { constructor(brand, mod) { super(brand); this.model = mod; } show() { return this.present() + ', it is a ' + this.model; } } mycar = new Model("Ford", "Mustang"); document.getElementById("demo").innerHTML = mycar.show();
Syntax
super(arguments); // Call the parent constructor (only in constructors) super.parentMethod(arguments); // Call the parent method
Technical Details
JavaScript Version: | ECMAScript 2015 (ES6) |
---|
Browser Support
Keyword | Chrome | IE | Firefox | Safari | Opera |
---|---|---|---|---|---|
super | 42.0 | 13.0 | 45.0 | 9.0 | 36.0 |
Related Pages
JavaScript Tutorial:JavaScript Class
JavaScript Tutorial:JavaScript ES6 (EcmaScript 2015)
JavaScript Reference Manual:extends keyword
JavaScript Reference Manual:constructor() Method
- Previous Page static
- Next Page constructor()
- Go to Parent Layer JavaScript Class Reference Manual