Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What are Constructors in JavaScript?

JavaScript is an incredibly powerful programming language, and one of its most useful features is the ability to create and manipulate objects. This blog post aims to introduce the concept of constructors in JavaScript, along with examples that will help beginners understand how to use them effectively.

To get started, let's first understand what objects are in the context of programming.

Objects: A Simple Analogy

Imagine you're a car manufacturer. You create various models of cars, each with specific attributes such as color, engine type, and number of doors. In programming, we can think of these car models as objects. They have properties (attributes) and can also perform actions, known in programming as methods.

In JavaScript, we can create objects using a simple syntax called object literals. Here's an example:

const car = {
  color: 'red',
  engineType: 'V8',
  numberOfDoors: 4,
  startEngine: function() {
    console.log('Engine started');
  },
};

In this example, car is an object with properties color, engineType, and numberOfDoors. It also has a method called startEngine, which logs a message to the console when called.

However, if we want to create multiple cars with different properties, using object literals can become cumbersome. This is where constructors come into play.

Introducing Constructors

A constructor in JavaScript is a special kind of function that allows us to create objects with a specific structure and behavior. Constructors act as a template for creating new objects, ensuring that each object has the same properties and methods.

To create a constructor, we use the function keyword followed by a name (usually starting with a capital letter to distinguish it from regular functions). Inside the constructor, we can define properties and methods using the this keyword.

Here's an example of a simple Car constructor:

function Car(color, engineType, numberOfDoors) {
  this.color = color;
  this.engineType = engineType;
  this.numberOfDoors = numberOfDoors;

  this.startEngine = function() {
    console.log('Engine started');
  };
}

Now, we can create new car objects using the new keyword, followed by the constructor name and the required arguments:

const car1 = new Car('red', 'V8', 4);
const car2 = new Car('blue', 'V6', 2);

With the constructor, we can easily create multiple car objects with different properties, without having to manually create new object literals for each one. This makes our code more efficient and easier to read.

Using Prototypes for Better Performance

In the previous example, the startEngine method is defined inside the constructor. This means that every time we create a new car object, a new copy of the startEngine method is created. This can be inefficient, especially if we have hundreds or thousands of car objects.

To improve performance, we can define methods using the constructor's prototype property. This allows all objects created by the constructor to share a single copy of the method, saving memory and improving performance.

Here's how we can modify our Car constructor to use prototypes:

function Car(color, engineType, numberOfDoors) {
  this.color = color;
  this.engineType = engineType;
  this.numberOfDoors = numberOfDoors;
}

Car.prototype.startEngine = function() {
  console.log('Engine started');
};

Now, whenever we create a new car object, it will inherit the startEngine method from the constructor's prototype, ensuring that only one copy of the method is created, no matter how many car objects we have.

ES6 Class Syntax

In 2015, a new version of JavaScript called ECMAScript 6 (ES6) was released, introducing a more concise syntax for creating constructors using the class keyword. Although the underlying concepts remain the same, the new syntax allows for cleaner and more readable code.

Here's how we can rewrite our Car constructor using the ES6 class syntax:

class Car {
  constructor(color, engineType, numberOfDoors) {
    this.color = color;
    this.engineType = engineType;
    this.numberOfDoors = numberOfDoors;
  }

  startEngine() {
    console.log('Engine started');
  }
}

To create a new car object, we use the new keyword just like before:

const car1 = new Car('red', 'V8', 4);

With the ES6 class syntax, methods are automatically added to the constructor's prototype, ensuring that only one copy is created for all objects.

Inheritance and Constructors

Another powerful feature of constructors is the ability to create new constructors that inherit properties and methods from a parent constructor. This is known as inheritance and allows us to create more specialized constructors without duplicating code.

For example, let's say we want to create a new constructor for electric cars that inherits from our existing Car constructor. We can do this using the extends keyword and the super function.

Here's an example using the ES6 class syntax:

class ElectricCar extends Car {
  constructor(color, numberOfDoors, batteryCapacity) {
    super(color, 'Electric', numberOfDoors);
    this.batteryCapacity = batteryCapacity;
  }

  chargeBattery() {
    console.log('Battery charging');
  }
}

In this example, we create a new ElectricCar constructor that inherits from the Car constructor. The super function is used to call the parent constructor, passing in the required arguments. We can then define additional properties and methods specific to electric cars, such as batteryCapacity and chargeBattery.

To create a new electric car object, we use the new keyword as before:

```javascript const electricCar1 = new ElectricCar('green', 4,