Constructors in JavaScript

Welcome to the exciting realm of constructors in JavaScript! Constructors are special functions that serve as blueprints for creating objects. They define the properties and behaviors that all instances (objects) created from them will share. This chapter delves into the fundamentals and advanced concepts of constructors, empowering you to craft well-structured and reusable objects in your JavaScript code.

The Basics: Creating Objects with Constructors

The new Keyword

  • The new keyword is the cornerstone of constructor usage. When used with a constructor function, it creates a new object, initializes it with the properties and methods defined within the constructor, and returns a reference to the newly created object.
				
					function Person(firstName, lastName) {
  this.firstName = firstName;
  this.lastName = lastName;
}

const person1 = new Person("Alice", "Smith");
console.log(person1);  // Output: Person { firstName: "Alice", lastName: "Smith" }

				
			

Explanation:

  • We define a constructor function named Person that takes two arguments: firstName and lastName.
  • Inside the constructor, this refers to the newly created object. We use this to assign the function arguments to the object’s properties (this.firstName and this.lastName).
  • When we use new Person("Alice", "Smith"), a new object is created with the provided values for firstName and lastName. The console.log statement displays the newly created object person1.

Constructor Properties and Methods

  • You can define additional properties and methods directly within the constructor function. These become part of the object’s blueprint and are accessible to all instances created from that constructor.
				
					function Car(model, year) {
  this.model = model;
  this.year = year;

  this.getDetails = function() {
    return `Model: ${this.model}, Year: ${this.year}`;
  };
}

const car1 = new Car("Camry", 2020);
console.log(car1.model);      // Output: Camry
console.log(car1.getDetails());  // Output: Model: Camry, Year: 2020

				
			

Explanation:

  • Here, the Car constructor defines two properties (model and year) and a method getDetails that returns a string with car details.
  • When we create a new Car object (car1), it inherits these properties and the method.
  • We can access the properties using dot notation and call the method to retrieve the car’s details.

Advanced Topics in Constructors

Prototype Inheritance

In JavaScript, objects inherit properties and methods from their prototype. The constructor function itself has a property named prototype that serves as the prototype for objects created using new

				
					function Animal(species) {
  this.species = species;
}

Animal.prototype.makeSound = function() {
  console.log("Generic animal sound");
};

const dog = new Animal("Dog");
dog.makeSound();  // Output: Generic animal sound

				
			

Explanation:

  • We define an Animal constructor with a species property.
  • We add a makeSound method to the Animal.prototype. This method becomes accessible to all objects created using Animal.
  • When we create a dog object, it inherits the makeSound method from the Animal prototype, even though it’s not defined directly in the Animal constructor.

Overriding Inherited Methods

  • You can override inherited methods from the prototype by defining the same method name within the constructor function itself. The object instance will use the method defined within its own constructor.
				
					function Dog(species) {
  this.species = species;
}

Dog.prototype.makeSound = function() {
  console.log("Woof!");
};

const dog = new Dog("Dog");
dog.makeSound();  // Output: Woof!

				
			

Explanation:

  • Here, the Dog constructor inherits the makeSound method from Animal.prototype.
  • However, we define a new makeSound method within the Dog constructor that specifically prints “Woof!”.
  • When we create a dog object, it uses the makeSound method defined directly in the Dog constructor, overriding the inherited one.

Constructors empower you to build reusable object blueprints in JavaScript. They provide structure, define initial properties and methods, and enable inheritance through prototypes. By understanding these concepts, you can create maintainable and efficient object-oriented applications Happy coding !❤️

Table of Contents

Contact here

Copyright © 2025 Diginode

Made with ❤️ in India