🔗 Prototype and Prototype Inheritance in JavaScript
Category: js / general-concepts
Difficulty: medium
Interview Importance: 🔴 Critical — Asked in 90% of JavaScript interviews. Understanding prototypes is fundamental to understanding how JavaScript works under the hood. 1️⃣ What is a Prototype? JavaScript uses prototype-based inheritance, meaning objects inherit properties and methods from other objects via the prototype chain — not through classical class-based inheritance like Java or C++. Core Concept Every JavaScript object has an internal link to another object called its prototype. When you access a property on an object, JavaScript first looks at the object itself. If not found, it looks up the prototype chain until it finds the property or reaches null. [code example] 2️⃣ Why Do Prototypes Matter? Memory Efficiency Without prototypes, each object instance would have its own copy of every method: [code example] Real-World Impact 1000 instances without prototype: 1000 function objects in memory 1000 instances with prototype: 1 shared function object 3️⃣ How Prototypes Work — Step by Step...