JavaScript | Super keyword
Super keyword in JavaScript can be used to access and call on an object’s parent, it can be used in two ways.
- As a function
- As an object
Syntax:
super(arguments); super.parentMethod(arguments);
Arguments: This keyword can accepts all the arguments that has been used to create a constructor.
Below example illustrates the uses of the super keyword in JavaScript.
Example: In the constructor of the FashionDesigner class, super has been used as a function. Whereas, in the doTasks() function of the FashionDesigner class, super has been used as an object. In the constructor of the fashion designer class, the super keyword has been used as a function to call the parent class’s constructor by passing the parameters to the fashion designer. This step is important to be carried out to ensure that FashionDesigner is an instance of Person. In the doTasks() function, super is used as an object that refers to the parent class Person’s instance. Here, the super keyword is used to explicitly call the methods of the parent class Person.
Javascript
<!DOCTYPE html> <html> <head> </head> <body> <script> class Person { constructor(name, age) { this.name = name; this.age = age; } atWork() { return this.name + " is at work, "; } atHome() { return this.name + " is at home"; } sleeping() { return this.name + " is sleeping"; } } class FashionDesigner extends Person { constructor(name, age) { super(name, age); } profession() { return this.name + " is a Fashion Designer"; } doTasks() { return super.atWork() + this.profession(); } } function display(content) { console.log(content); } const charecter = new FashionDesigner("Sayan", 30); display(charecter.profession()); display(charecter.atHome()); display(charecter.doTasks()); </script> </body> </html> |
Output:
Sayan is a Fashion Designer Sayanis at home Sayanis at work Sayan is a Fashion Designer
Recommended Posts:
- JavaScript | new Keyword
- What's the yield keyword in JavaScript?
- Difference between Primary key and Super key
- Calling A Super Class Constructor in Scala
- PHP | var keyword
- Final keyword in PHP
- CSS | unset keyword
- ES6 | Void Keyword
- CSS | revert keyword
- CSS | currentcolor keyword
- Range Keyword in Golang
- Throw Keyword in Scala
- How to implement is functionality without using is keyword in C#
- Global keyword in Python
- How to instantiate Struct using new keyword in Golang?
- Invoking an overloaded constructor using this keyword in C#
- Golang program that uses defer keyword
- Golang program that uses fallthrough keyword
- Difference between revert and unset keyword in CSS
- Introduction to JavaScript Course | Learn how to Build a task tracker using JavaScript
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


