Welcome to our deep dive into JavaScript Prototype Chain! Let's explore this powerful feature that makes JavaScript a dynamic and flexible programming language. This tutorial is perfect for beginners and intermediates alike, so let's get started!
In JavaScript, every object has a prototype, which is another object that contains properties and methods. When you try to access a property or a method that doesn't exist on an object, JavaScript looks for it in the object's prototype and continues this search up the prototype chain until it finds the property or method or reaches the end.
Let's create a simple constructor to understand the prototype chain better:
function Car(make, model) {
this.make = make;
this.model = model;
}
// Creating an object using the constructor
let myCar = new Car('Toyota', 'Corolla');In the above example, the Car function is a constructor that creates an object with make and model properties. The myCar object is created using the new keyword and inherits properties from the Car constructor's prototype.
Let's dive deeper into the prototype chain:
console.log(myCar.__proto__ === Car.prototype); // trueIn the above example, we confirm that the myCar object's prototype is indeed the Car.prototype object. Now, let's check what's in the Car.prototype object:
console.log(Car.prototype);
// Output: {constructor: ƒ}The Car.prototype object has a constructor property, which points back to the constructor function. But what if we try to access a property that doesn't exist on our myCar object?
console.log(myCar.wheels); // undefinedSince the myCar object doesn't have a wheels property, JavaScript looks for it in the Car.prototype object:
console.log(Car.prototype.hasOwnProperty('wheels')); // falseSince the wheels property doesn't exist in the Car.prototype, JavaScript continues its search up the prototype chain. If we keep going, we'll eventually reach the Object constructor's prototype, which has a hasOwnProperty method and several other methods and properties.
The prototype chain in JavaScript provides a way for objects to inherit properties and methods from other objects. This allows us to create efficient and reusable code. For example, we can create a Vehicle constructor with common properties and methods, and then create specific constructors like Car and Bike that inherit from Vehicle.
Let's create a Vehicle constructor with a drive method:
function Vehicle(make, model) {
this.make = make;
this.model = model;
}
Vehicle.prototype.drive = function() {
console.log(`Driving ${this.make} ${this.model}`);
}
// Creating Car and Bike constructors that inherit from Vehicle
function Car(make, model) {
this.numWheels = 4;
Vehicle.call(this, make, model);
}
Car.prototype = Object.create(Vehicle.prototype);
Car.prototype.constructor = Car;
function Bike(make, model) {
this.numWheels = 2;
Vehicle.call(this, make, model);
}
Bike.prototype = Object.create(Vehicle.prototype);
Bike.prototype.constructor = Bike;In the above example, we've created a Vehicle constructor with a drive method. The Car and Bike constructors inherit from Vehicle, and we've set up their prototypes to inherit from Vehicle.prototype.
Let's create instances of Car and Bike and test the drive method:
let myCar = new Car('Toyota', 'Corolla');
let myBike = new Bike('Honda', 'Civic');
myCar.drive(); // Driving Toyota Corolla
myBike.drive(); // Driving Honda CivicIn the above example, we've created instances of Car and Bike, and called the drive method on both. Since the drive method is part of the Vehicle.prototype, both Car and Bike instances have access to it.
What is the prototype chain in JavaScript?
That's it for our deep dive into JavaScript Prototype Chain! Now you should have a good understanding of how the prototype chain works and how you can leverage it to create efficient and reusable code. Happy coding! 🚀