Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What is a constructor in JavaScript

Understanding the Concept of a Constructor

Imagine you are an architect and you have been tasked with designing and building a series of similar-looking houses. Instead of creating each house from scratch, you would make a blueprint. This blueprint would then be used each time a new house is to be constructed, ensuring that each house follows the same structure and design.

In JavaScript, a constructor functions much like this blueprint. It provides the structure for creating objects of a similar type.

The Basics of Constructors in JavaScript

Constructors are special functions that are used to initialize new objects of a given type. They are named after the type of object they create. For instance, if we have a Car type, the constructor function for creating new cars would be Car().

Here's a basic example:

function Car(make, model, year) {
  this.make = make;
  this.model = model;
  this.year = year;
}

var myCar = new Car('Toyota', 'Corolla', 2005);
console.log(myCar);
// Output: Car { make: 'Toyota', model: 'Corolla', year: 2005 }

In this example, function Car(make, model, year) is our constructor function. We use it to create a new Car object with the new keyword.

Digging Deeper: The this Keyword

In our Car constructor function, you may have noticed the use of this. The this keyword in JavaScript refers to the object that the function is a property of. In the case of a constructor function, this refers to the newly created object.

When we say this.make = make;, we are essentially saying "the make property of this new car object should be set to the make value that we pass into the Car function".

Let's add a method to our Car constructor to see this in action:

function Car(make, model, year) {
  this.make = make;
  this.model = model;
  this.year = year;
  this.displayCar = function() {
    return this.make + ' ' + this.model;
  }
}

var myCar = new Car('Toyota', 'Corolla', 2005);
console.log(myCar.displayCar());
// Output: Toyota Corolla

In this case, this inside of the displayCar function refers to the car object that the function is being called on.

The new Keyword and Prototypes

The new keyword in JavaScript creates a new instance of an object. It's like saying, "Hey, JavaScript, make me a new Car object using this Car constructor function."

When we use the new keyword with a constructor function, JavaScript does a few things under the hood:

  1. It creates a new, empty object.
  2. It sets the prototype of this new object to the constructor function's prototype.
  3. It calls the constructor function with this set to the newly created object.
  4. It returns the newly created object.

The prototype mentioned in step 2 is important. Every JavaScript function has a prototype property that is used for inheritance. When an object is created using a constructor function, it inherits all the properties and methods from the constructor's prototype.

Let's add a method to the Car prototype:

function Car(make, model, year) {
  this.make = make;
  this.model = model;
  this.year = year;
}

Car.prototype.displayCar = function() {
  return this.make + ' ' + this.model;
}

var myCar = new Car('Toyota', 'Corolla', 2005);
console.log(myCar.displayCar());
// Output: Toyota Corolla

In this example, the displayCar method is added to the Car prototype. This means that all Car objects will have access to this method, not just myCar.

Conclusion: The Power of Constructors

So, constructors are like blueprints that allow us to create objects of a specific type with their own properties and methods. They give us a way to create multiple objects that follow the same structure, much like building houses from a blueprint.

But what makes constructors truly powerful is their ability to work with the new keyword and prototypes to enable object creation, function execution, and inheritance all in one go.

Remember our Car constructor? With it, we can create as many cars as we want, each with its own make, model, and year, and each able to use the displayCar method. It's like having a car factory at our disposal, ready to produce cars on demand.

So next time you're tasked with creating multiple similar objects, consider using a constructor. It's an efficient, effective way to build objects and one of the many tools that make JavaScript such a versatile language. Happy coding!