Constructor is just a function to create custom object type and create object’s instances using new
keyword.
/**
* `Constructor function` that defines custom data object, commonly named using capital letter.
* The properties will be available for every object created from this function,
* because `this` points to the instance of the object.
*/
function Human(name, lastName) {
this.name = name;
this.lastName = name;
}
/**
* Objects are created from `constructor function` using `new` keyword.
* Parameters that are passed to the function will be the values of object's properties.
*/
const american = new Human('Mike', 'Brown');
Inside classes the constructor
method is actually constructor function - what creates instances of an object.
class AnotherHuman{
constructor(name, age){
this.name = name;
this.age = age;
}
const student = AnotherHuman('Jack', 22);