CONSTRUCTOR
A constructor is a special method that gets called when an object is instantiated from a class. It is responsible for initializing the object's properties or performing any setup that is necessary for the object to function correctly.
In JavaScript, you can create classes using either constructor functions or ES6 class syntax. Here's an example using the ES6 class syntax:
1class Person {
2 constructor(name, age) {
3 this.name = name;
4 this.age = age;
5 }
6}
7
In this example, Person is a class, and constructor(name, age) is the constructor method.
To create a new object using the constructor function, you use the new keyword followed by the function name and any required parameters:
1let person1 = new Person("John", 30);
2let person2 = new Person("Jane", 25);
The this keyword inside the constructor function refers to the current object being created. The this keyword is used to assign the properties passed in as arguments to the new object being created.