In object-oriented programming (OOP), one of the fundamental concepts is creating multiple instances of an object with similar properties and behaviors. In JavaScript, constructor functions provide a way to create objects using a blueprint or template. This tutorial will guide you through understanding how to use constructor functions along with the new keyword to instantiate multiple objects efficiently.
Constructor functions in JavaScript are regular functions that are used to initialize new objects. When called with the new keyword, they perform several actions:
this keyword inside the function refers to this newly created object.Constructor functions are typically named with an uppercase first letter to distinguish them from regular functions, although JavaScript does not enforce this convention.
Let's start by creating a simple constructor function for a Car object:
1function Car(make, model, year) {2this.make = make;3this.model = model;4this.year = year;5}
In this example, the Car constructor function takes three parameters: make, model, and year. These parameters are used to set the properties of the new object.
To create a new instance of a Car using the constructor function, you use the new keyword:
1const myCar = new Car('Toyota', 'Corolla', 2020);2console.log(myCar.make); // Output: Toyota3console.log(myCar.model); // Output: Corolla4console.log(myCar.year); // Output: 2020
Toyota Corolla 2020
When you use the new keyword, JavaScript automatically sets up a new object and assigns it to this inside the constructor function. This allows you to add properties and methods to the object.
You can also add methods to objects created by a constructor function. Here's an example of adding a method called displayInfo:
1function Car(make, model, year) {2this.make = make;3this.model = model;4this.year = year;56this.displayInfo = function() {7console.log(`${this.year} ${this.make} ${this.model}`);8};9}1011const myCar = new Car('Toyota', 'Corolla', 2020);12myCar.displayInfo(); // Output: 2020 Toyota Corolla
2020 Toyota Corolla
In this example, the displayInfo method is added directly to each instance of the Car. While this works, it's not efficient because each new object has its own copy of the method. This can lead to memory inefficiencies.
To avoid creating multiple copies of methods for each object, you can use prototypes. Prototypes are shared among all instances of a constructor function. We'll cover this in more detail in the next section, "JavaScript Prototypes."
Let's create a practical example to demonstrate how constructor functions can be used to manage a simple library system.
1function Book(title, author) {2this.title = title;3this.author = author;45this.displayInfo = function() {6console.log(`${this.title} by ${this.author}`);7};8}910const book1 = new Book('1984', 'George Orwell');11const book2 = new Book('To Kill a Mockingbird', 'Harper Lee');1213book1.displayInfo(); // Output: 1984 by George Orwell14book2.displayInfo(); // Output: To Kill a Mockingbird by Harper Lee
1984 by George Orwell To Kill a Mockingbird by Harper Lee
In this example, the Book constructor function is used to create multiple book objects. Each book has its own title and author, but they share the same displayInfo method.
new keyword is used to instantiate a new object from a constructor function.In the next tutorial, we'll explore JavaScript prototypes in detail. Prototypes allow you to share properties and methods among all instances of a constructor function, making your code more efficient and organized. Stay tuned!