How to fix ReferenceError – Super constructor may only be called once in JavaScript?
Last Updated :
13 Aug, 2024
In JavaScript "ReferenceError: Super constructor may only be called once" error occurs when you call super more than once in a derived class constructor, the super keyword calls the parent class constructor and must be called only once, we can see this error, reasons behind it as well as how to correct it with examples.
Understanding an error
"Super constructor may only be called once" is a message that shows when the super keyword is used more than once in one constructor of a child class, to use “this” keyword, the superclass constructor needs to be specified immediately after calling super.
Case 1: Error Cause: Calling super
Multiple Times in Constructor
Example: You will see this error when you are calling the super keyword more than once in a class constructor.
class ParentClass {
constructor() {
console.log('Parent class constructor');
}
}
class ChildClass extends ParentClass {
constructor() {
super();
super(); // Second call to super() causes error
console.log('Child class constructor');
}
}
const childInstance = new ChildClass();
Output:
ReferenceError: Super constructor may only be called once
Resolution of error
To avoid such kind of error you should ensure that super is called only once in the constructor of the derived class.
JavaScript
class ParentClass {
constructor() {
console.log('Parent class constructor');
}
}
class ChildClass extends ParentClass {
constructor() {
super();
console.log('Child class constructor');
}
}
const childInstance = new ChildClass();
OutputParent class constructor
Child class constructor
Case 2: Error Cause: Conditional Super Calls
Example: When you are using conditional logic which results multiple calls of super can cause this error.
class ParentClass {
constructor() {
console.log('Parent class constructor');
}
}
class ChildClass extends ParentClass {
constructor(condition) {
if (condition) {
super(); // First call to super()
}
super();
// Second call to super(), causes ReferenceError
console.log('Child class constructor');
}
}
const childInstance = new ChildClass(true);
Output:
ReferenceError: Super constructor may only be called once
Resolution of error
To avoid such kind of error you should ensure that you are using super only once regardless of the conditional logic.
JavaScript
class ParentClass {
constructor() {
console.log('Parent class constructor');
}
}
class ChildClass extends ParentClass {
constructor(condition) {
super(); // Call super() once
if (condition) {
console.log('Condition is true');
} else {
console.log('Condition is false');
}
console.log('Child class constructor');
}
}
const childInstance = new ChildClass(true);
OutputParent class constructor
Condition is true
Child class constructor
Conclusion
In conclusion to avoid Super constructor may only be called once errors in JavaScript, make sure you call the super keyword exactly once within a derived class’s constructor, this strictly adheres to JavaScript’s way of inheriting classes thereby ensuring proper initialization of the parent class and how this should function within the derived one.
Similar Reads
How to Fix "ReferenceError: document is not defined" in JavaScript? The "ReferenceError: document is not defined" error in JavaScript is a common issue that occurs when trying to access the document object outside the browser environment such as in Node.js. This error can also occur if the script is executed before the HTML document is fully loaded. In this article,
2 min read
How to call the constructor of a parent class in JavaScript ? In this article, we learn how to call the constructor of a parent class. Before the beginning of this article, we should have a basic knowledge of javascript and some basic concepts of inheritance in javascript. Constructor: Constructors create instances of a class, which are commonly referred to as
4 min read
Function that can be called only once in JavaScript In JavaScript, you can create a function that can be called only once by using a closure to keep track of whether the function has been called before. JavaScript closure is a feature that allows inner functions to access the outer scope of a function. Closure helps in binding a function to its outer
3 min read
JavaScript TypeError - "X" is not a constructor This JavaScript exception is not a constructor that occurs if the code tries to use an object or a variable as a constructor, which is not a constructor. Message: TypeError: Object doesn't support this action (Edge) TypeError: "x" is not a constructor TypeError: Math is not a constructor TypeError:
1 min read
JavaScript ReferenceError Deprecated caller or arguments usage This JavaScript exception deprecated caller or arguments usage occurs only in strict mode. It occurs if any of the Function.caller or Function.arguments properties are used, Which are depreciated. Message: TypeError: 'arguments', 'callee' and 'caller' are restricted function properties and cannot be
1 min read
How to call a parent method from child class in JavaScript? Inheritance in programming allows a class to derive methods and properties from another class, similar to how a child inherits traits from its parents. A child class can have its properties in addition to inherited ones. The deriving class is called a derived, sub, or child class, while the class it
3 min read