InterviewHubPRO
Section Overview
Q261

Question 261 of 521

Part 5: ES6+ Modern Syntax, Classes, Modules & Destructuring (254 - 328)

50%
JavaScript Internals (521 Qs)•Part 5: ES6+ Modern Syntax, Classes, Modules & Destructuring (254 - 328)

What happens if you write constructor more than once in a class

The "constructor" in a class is a special method and it should be defined only once in a class. i.e, If you write a constructor method more than once in a class it will throw a SyntaxError error.

javascript
   class Employee {
     constructor() {
       this.name = "John";
     }
     constructor() {   //  Uncaught SyntaxError: A class may only have one constructor
       this.age = 30;
     }
   }

   var employeeObject = new Employee();

   console.log(employeeObject.name);

This constructor is called by using the special function call new (see example above).