super Keyword in JavaScript – What Is the super Keyword?
The super
keyword searches a parent class or object literal for a specified static or prototypal property.
For instance, consider the following snippet:
The super()
function call in the snippet above tells the computer to find a constructor
in the parent class’s prototype chain.
You can use the super
keyword as a “function caller” or “property accessor”. Let’s discuss how.
How to Use the super
Keyword as a Function Caller
The super()
function caller finds and invokes the parent class’s constructor()
method.
In other words, super()
allows you to access a parent class’s constructor
from the constructor
of a derived class.
Syntax of the super
keyword as a function caller
Example: How to use the super()
function caller
The super()
function call in the snippet above tells the computer to find and invoke the parent class’s constructor()
.
In other words, the super()
function call searches for a constructor
in Name
’s prototype chain.
Note the following:
- Calling
super()
allows JavaScript to use the parent class’sconstructor
to initializethis
. So, asuper()
function call is similar to writingthis = new ParentClass()
. - JavaScript requires you to call
super()
before using the keywordthis
. Otherwise, the browser will throw aReferenceError
. In other words, a derived class’sconstructor
cannot access an uninitialized keywordthis
.
Example: What happens if you access this
before super
in a derived class’s constructor
?
The snippet above throws an Uncaught ReferenceError
because a derived class’s constructor
cannot access the keyword this
before the super()
function caller.
Example: What happens if you use only this
keyword in a derived class’s constructor
?
The snippet above throws an Uncaught ReferenceError
because a derived class’s constructor
cannot access the keyword this
before the super()
function caller.
Now that we know how to use the super
keyword as a function caller, we can discuss using it as a property accessor.
How to Use the super
Keyword as a Property Accessor
You can use the super
keyword as a property accessor in your JavaScript classes and object literals.
- Class Usage: The
super
keyword searches a class’s parent for a specified static or prototypal class field. In other words,super
allows you to access a parent class’s static or prototypal properties from a child class. - Object Literal Usage: The
super
keyword searches an object’s parent for a specified prototypal property. In other words,super
allows you to access a parent object’s prototypal properties from a child object.
Syntax of the super
keyword as a dot notation property accessor
Example: Use the super
keyword’s dot notation to access the parent class’s static field
We used the super
keyword in the snippet above to access the parent class’s static class field.
Create your web presence in no time
Example: Use the super
keyword’s dot notation to access the parent class’s prototypal field
We used the super
keyword in the snippet above to access the parent class’s prototypal class fields.
Example: Use the super
keyword’s dot notation to access a parent object’s prototypal property
We used the super
keyword in the snippet above to access the parent object’s showName()
method.
You can also use the super
keyword as a bracket notation property accessor to search a parent class or object literal for a specified static or prototypal property.
Syntax of the super
keyword as a bracket notation property accessor
Example: Use the super
keyword’s bracket notation to access a parent class’s static field
We used the super
keyword in the snippet above to access the parent class’s static class field.
Note: super
cannot access a parent class’s instance class field because JavaScript sets an instance property on the object instance, not the class itself or its prototype chain. (super
searches only for a parent’s static or prototypal properties.)
Express Your Love for Coding!
Example: Use the super
keyword to access the parent class’s instance field
The firstName
property’s value is undefined
because super
could not find a prototypal myName
field on the parent class.
Note: The keywords super
and this
allow you to search for a specified property in an object’s prototype chain. But they work in different ways. Let’s discuss their differences now.
super
vs. this
Keyword: What’s the Difference?
The difference between the super
and this
keyword is as follows:
super
searches for a specified prototypal property in a parent class’s prototype chain.this
searches for a specified prototypal property from a class’s object instance’s own properties to its prototype chain.
In other words, super
starts its search from the parent class’s prototype
property. But this
searches from an object instance’s local scope to its prototype chain.
For instance, consider the following code:
Here’s how super
and this
performed their searches:
super | this | |
---|---|---|
1. | Find | Find |
2. | (Suppose | Find |
3. | (Suppose | |
4. | (Suppose |
You can see that super
shortens the steps required to find a prototypal method.