Understanding JavaScript’s Prototype

Rishabh
JavaScript Journal: Unlocking Project Potential
2 min readJun 15, 2024

Once upon a time, in the charming village of JavaScript Nagar, there lived many objects. These objects were not ordinary; they had special powers. But the true magic lay in their connections, forming what the villagers called the Prototype Chain.

The Wise Elder

In this village, there was an elder named Object.prototype. He was the wisest and oldest of all objects, the great ancestor from whom all objects descended. Every object in Javascript Nagar could trace its lineage to Object.prototype.

The Curious Case of mukesh

One day, a new object was born. His name was mukesh. He was simple object but curious, always looking to learn more about his abilities. He knew he had inherited some traits from his ancestors, but he wasn’t sure how.

let mukesh = {};

The Quest for Knowledge

mukesh decided to go on a quest to understand his heritage. He knew that when he tried to use a property or method, Javascript Nagar’s magic would first check if he had it. If he didn’t, the magic would move up to his parent, and then his parent’s parent, and so on, until it reached Object.prototype.

console.log(mukesh.__proto__ === Object.prototype); // true

The Magical Chain

In his journey, mukesh discovered that he had a special property called __proto__. This property was like a map leading to his parent. If his parent didn’t have the answer, the map would lead further up the chain, eventually reaching the Object.prototype.

console.log(mukesh.toString); // Inherited from Object.prototype
console.log(mukesh.__proto__.__proto__ === null); // true

Meeting the Constructor

During his adventure, mukesh met many others like him, created using blueprints called constructors. One such constructor was Array. Objects created by Array had a different set of inherited methods and properties.

let myArray = [];
console.log(myArray.__proto__ === Array.prototype); // true
console.log(Array.prototype.__proto__ === Object.prototype); // true

myArray knew about methods like push and pop, which he got from Array.prototype. But if myArray didn’t have a method, the search would go from Array.prototype and up to Object.prototype.

The Endless Exploration

As mukesh explored, he realised that the Prototype Chain was like an ancient family tree, full of wisdom and knowledge passed down through generations. This chain allowed objects to share abilities without duplicating them, making the village of JavaScriptia efficient and harmonious.

The Moral of the Story

In Javascript Nagar, every object has a lineage, a chain of prototypes that links it back to the ancient Object.prototype. This chain is the source of shared properties and methods, enabling powerful inheritance and reusability.

Understanding the Prototype Chain helps us appreciate the beauty of JavaScript’s inheritance model, making us better coders and storytellers in our own right.

--

--

Rishabh
JavaScript Journal: Unlocking Project Potential

👋 React wizard by day, guitar hero by night. I write code that even my bugs get applause! On a quest to make UI/UX spell "U I'm Excited!" 🎸🤓