DEV Community

Cover image for Understanding JavaScript Prototypes and Prototypal Inheritance: The Easiest Guide Ever
Daniel Olawoyin
Daniel Olawoyin

Posted on

Understanding JavaScript Prototypes and Prototypal Inheritance: The Easiest Guide Ever

Hey there! So, you’re here because you’ve heard of prototypes and prototypal inheritance in JavaScript, but you're not sure what they are or how they work. No worries, you’re not alone! These concepts can sound complicated at first, but with the right explanation, you’ll have a lightbulb moment, and it’ll all make sense. Let’s break this down in the easiest way possible, without all the confusing tech terms.

What are Prototypes in JavaScript?

Imagine you have a bunch of people who all know the same stuff. They can share knowledge with each other without needing to learn everything from scratch. That’s kind of how prototypes work in JavaScript. A prototype is like a “blueprint” that objects can refer to. When you create an object, it can “inherit” or “borrow” properties and methods (functions) from another object. This happens through the prototype.

Let’s take a simple example to understand this better:

// Let's create an object called "car"
let car = {
    brand: "Toyota",
    drive() {
        console.log("Vroom vroom!");
    }
};
Enter fullscreen mode Exit fullscreen mode

Here, we have an object car with a property brand and a method drive. But what if we want to create other cars with the same properties, like a Honda or a Ford?

Instead of writing the same properties and methods over and over again, we can use prototypes to help share them.

The Magic of Prototypes

In JavaScript, every object has a prototype, which is another object that it can "inherit" properties and methods from. The prototype is like the object’s parent. When you try to access a property or method on an object, JavaScript first looks at the object itself. If it doesn’t find it there, it checks the prototype. If it still doesn’t find it, it looks at the prototype of that prototype, and so on. This is how JavaScript’s prototype chain works.

Let’s look at an example to explain this:

let car = {
    brand: "Toyota",
    drive() {
        console.log("Vroom vroom!");
    }
};

// This is another car object that doesn't have its own `drive` method
let myCar = Object.create(car);

myCar.brand = "Honda";  // We give myCar its own brand

myCar.drive();  // Outputs: Vroom vroom!
Enter fullscreen mode Exit fullscreen mode

In this example, myCar is created using Object.create(car). This means that myCar doesn’t have its own drive method. So, it looks at the car prototype (which has the drive method) and uses it. This is how prototypal inheritance works: the myCar object inherits the drive method from car.

Prototypal Inheritance: A Little Deeper Dive

Now that we understand prototypes, let’s talk about prototypal inheritance. This is simply the way objects can inherit properties and methods from other objects. In the example above, myCar inherited the drive method from car.

Think of prototypal inheritance as a family tree. If myCar is a child of car, it can inherit stuff (like the drive method) from its parent. But if myCar doesn’t have something itself, it looks to the next generation (its prototype) for it.

Here’s an example that shows how inheritance works in a chain:

let animal = {
    eat() {
        console.log("Eating...");
    }
};

let dog = Object.create(animal);  // dog is created from animal

let myDog = Object.create(dog);  // myDog is created from dog

myDog.eat();  // Outputs: Eating...

Enter fullscreen mode Exit fullscreen mode

In this example:

  • myDog is created from dog, and dog is created from animal.
  • Even though myDog doesn’t have the eat method, it can still use it because it’s inherited from dog, and dog inherited it from animal.

Why Should You Care About Prototypes?

At first glance, prototypes and inheritance might seem like just extra stuff you don’t need to worry about. But understanding these concepts is actually pretty important for becoming a JavaScript master. Here's why:

  1. Efficiency: Prototypes allow you to avoid duplicating code. Instead of having to define the same methods and properties in every object, you can define them once in a prototype and let multiple objects use them.
  2. Better Memory Management: Objects don’t keep their methods and properties; they link to them through the prototype. This can save memory, especially when you’re working with many objects.
  3. Dynamic Behavior: You can change an object’s prototype at runtime, allowing you to change the behavior of an object dynamically without modifying the object itself.

How to Work with Prototypes

To work with prototypes, there are a few common methods and properties to remember:

Object.create()
This method is used to create a new object and set its prototype. For example:

let newObject = Object.create(prototypeObject);

Enter fullscreen mode Exit fullscreen mode

This creates newObject with the prototype of prototypeObject.

__proto__
Every object has a __proto__ property that points to its prototype. You can see it in action like this:

console.log(myCar.__proto__);  // Shows the prototype of myCar

Enter fullscreen mode Exit fullscreen mode

Constructor Functions
When you create a new object using a function, the function automatically sets up the prototype for the new object.

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

Animal.prototype.sayHi = function() {
    console.log(`Hi, I'm ${this.name}`);
};

let dog = new Animal("Buddy");
dog.sayHi();  // Outputs: Hi, I'm Buddy


Enter fullscreen mode Exit fullscreen mode

Here, the Animal function acts as a constructor, and all instances of Animal (like dog) inherit from Animal.prototype.


We’ve now learned that prototypes in JavaScript are like blueprints or family trees. They help objects share methods and properties with each other without repeating code. Prototypal inheritance makes objects dynamic, efficient, and memory-friendly by letting them inherit behavior from other objects.

I hope this explanation was as clear as day? If something was unclear, feel free to reach out and ask questions.

Happy Coding! 🚀

Top comments (0)