Skip to main content

Command Palette

Search for a command to run...

The Magic of JavaScript Prototypes: Like a Family Tree!

JavaScript Prototype Explained Like a Family Tree!

Updated
3 min read
The Magic of JavaScript Prototypes: Like a Family Tree!

If you’ve ever found JavaScript’s prototype system confusing, don’t worry—you’re not alone! But what if I told you that understanding it is as simple as looking at a family tree? That’s right! Let’s break it down in a fun and relatable way.

🧐 What is a Prototype?

A prototype in JavaScript is like a hidden blueprint that every object has. It’s a special place where shared properties and methods are stored. Instead of giving each object its own copy, JavaScript lets multiple objects share the same functions using prototypes.

Now lets understand it as a family tree :

Imagine JavaScript objects as family members. Just like kids inherit traits from their parents, objects in JavaScript inherit properties and methods from their prototypes. Here’s how it works:

1. The Grandparent: Object.prototype

At the very top of the family tree is Object.prototype, the ultimate ancestor of all JavaScript objects. Every object in JavaScript inherits from it, making it the "grandparent" of all objects.

console.log({}.toString()); // [object Object]

This toString() method is inherited from Object.prototype!

2. The Parent: Constructor Functions

Constructor functions act like parents in our family tree. When you create an object using a constructor function, it inherits methods from that function’s prototype.

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

Person.prototype.sayHello = function() {
  console.log(`Hello, my name is ${this.name}!`);
};

const alice = new Person("Alice");
alice.sayHello(); // Hello, my name is Alice!

Here, Person.prototype is like a parent passing down the sayHello method to its child, alice.

3. The Child: Instance Objects

When we create a new instance using new Person("Alice"), Alice inherits everything from Person.prototype. If Alice doesn’t have a property or method, JavaScript will look up the chain until it finds one.

console.log(alice.toString()); // Still works because it comes from Object.prototype

4. The Siblings: Objects with the Same Prototype

All objects created from the same constructor function share the same prototype—just like siblings share the same parents!

const bob = new Person("Bob");
bob.sayHello(); // Hello, my name is Bob!

Both alice and bob share Person.prototype, just like siblings sharing inherited traits.

5. Adopting Traits: Prototype Chaining

If a child (instance) doesn’t have a property, JavaScript looks up the chain. This is known as prototype chaining.

console.log(alice.hasOwnProperty("sayHello")); // false (because it's inherited from prototype)

The method hasOwnProperty comes from Object.prototype, the grandparent!

6. Breaking the Chain: Object.create()

Sometimes, you might want an object to have a specific prototype without using a constructor function. Think of it as adopting a child into a different family!

const superhero = Object.create(alice);
superhero.superPower = "Flying";

console.log(superhero.name); // Alice (inherited)
console.log(superhero.superPower); // Flying (own property)

Conclusion

JavaScript’s prototype system is just like a family tree. Objects inherit properties from their prototypes, which can be traced back up the chain to Object.prototype. Understanding this can make working with JavaScript objects much easier!

So next time you're debugging an object, just ask yourself: Who are its parents? What did it inherit?