Advertisement
728x90
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
1class Employee {
2 constructor() {
3 this.name = "John";
4 }
5 constructor() { // Uncaught SyntaxError: A class may only have one constructor
6 this.age = 30;
7 }
8 }
9
10var employeeObject = new Employee();
11
12console.log(employeeObject.name);This constructor is called by using the special function call new (see example above).
Advertisement
Responsive Ad
🎯 Practice NowRelated Challenge
JavaScript Coding Exercise 86
Test your knowledge with this interactive coding challenge.
Start CodingAdvertisement
728x90
257of476