FrontendDeveloper.in

JavaScript question detail

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.

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).

Back to all JavaScript questions
Get LinkedIn Premium at Rs 399