Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What is prototype chain in JavaScript

What is Prototype Chain in JavaScript?

Getting Started with Prototypes

Before we dive into the depths of prototype chains in JavaScript, let's briefly discuss prototypes. In the simplest terms, a prototype is a blueprint object that other objects inherit properties and methods from. If you're new to programming, you might compare this with a recipe you inherited from your grandmother. Just as you can add your own twist to grandma's recipe, objects can have properties and methods that are unique to them, apart from what they inherit from their prototype.

Here's a simple example to illustrate this:

let animal = {
  type: 'mammal',
  describe: function() {
    return `I am a ${this.type}`;
  }
};

let dog = Object.create(animal);
dog.type = 'dog';

console.log(dog.describe()); // prints: I am a dog

In this example, dog inherits properties and methods from the animal prototype, but it overrides the type property.

The Magic of the Prototype Chain

Now that we understand prototypes, let's delve into the prototype chain. The prototype chain is a mechanism that JavaScript uses to look up properties and methods of an object. If a property or method is not found on the object itself, JavaScript looks up (or "traverses") the prototype chain to find it.

Think of the prototype chain as a family tree. If you don't know the meaning of a word, you might ask your parents. If they don't know, they might ask your grandparents, and so on. This is similar to how JavaScript traverses the prototype chain to find properties and methods.

Here's an example to illustrate the prototype chain:

let animal = {
  type: 'mammal',
  describe: function() {
    return `I am a ${this.type}`;
  }
};

let dog = Object.create(animal);
dog.name = 'Spot';
dog.describeName = function() {
  return `My name is ${this.name}`;
}

console.log(dog.describe()); // prints: I am a mammal
console.log(dog.describeName()); // prints: My name is Spot

In this case, when we call dog.describe(), JavaScript first looks for a describe method on the dog object. It doesn't find one, so it moves up the prototype chain and finds the describe method on the animal prototype. This is why dog.describe() prints 'I am a mammal'.

A Deeper Dive into Prototype Chains

One of the most fascinating aspects of prototype chains is that they can be as long as needed. This means that an object can inherit from another object, which inherits from another object, and so on. This is similar to how you might have inherited certain traits from your parents, who inherited them from your grandparents, and so on.

Here's an example that illustrates this:

let creature = {
  alive: true
};

let animal = Object.create(creature);
animal.type = 'mammal';

let dog = Object.create(animal);
dog.name = 'Spot';

console.log(dog.alive); // prints: true

In this case, when we call dog.alive, JavaScript first looks for an alive property on the dog object. It doesn't find one, so it moves up the prototype chain and looks on the animal object. It doesn't find the property there either, so it continues up the prototype chain and finally finds the alive property on the creature prototype.

Conclusion

Understanding the prototype chain is like unlocking a secret level in a video game. It gives you a deeper understanding of JavaScript and opens up a world of possibilities for code reuse and object-oriented programming. Just like your ancestry gives you a sense of identity and belonging, the prototype chain provides a similar context for objects in JavaScript. They aren't just standalone entities, but part of a chain, a lineage of sorts, that lends them their unique characteristics.

So the next time you're working with objects in JavaScript, remember that they're part of a bigger family. They have an inheritance, a lineage, a prototype chain, that gives them their power. It’s not just about the properties and methods they personally hold but also about what they inherit from their prototype ancestors. Embrace the prototype chain, and you'll open up a new understanding of the rich and powerful language that is JavaScript.