JavaScript Inheritance: Prototypal vs Classical vs ES6

Most developers think inheritance in JavaScript works like traditional OOP. It doesn’t. And that confusion leads to messy, over-engineered code. JavaScript uses prototypal inheritance — not classical inheritance. 👉 Objects inherit directly from other objects. 💡 There are 3 main ways inheritance works in JavaScript: 🔹 1. Prototypal Inheritance (Core Concept) const animal = { speak() { console.log("Makes a sound"); } }; const dog = Object.create(animal); dog.bark = function () { console.log("Bark"); }; dog.speak(); // inherited dog.bark(); // own method ✔ Simple ✔ Flexible ✔ Native to JS 🔹 2. Constructor Function Inheritance function Animal(name) { this.name = name; } Animal.prototype.speak = function () { console.log(this.name + " makes noise"); }; function Dog(name) { Animal.call(this, name); // inherit properties } Dog.prototype = Object.create(Animal.prototype); const d = new Dog("Tommy"); d.speak(); ✔ More structured ✔ Used in older codebases 🔹 3. Class-based Inheritance (ES6) class Animal { constructor(name) { this.name = name; } speak() { console.log(this.name + " makes noise"); } } class Dog extends Animal { bark() { console.log("Bark"); } } const dog = new Dog("Rocky"); dog.speak(); dog.bark(); ✔ Cleaner syntax ✔ Easier to read ❗ Still uses prototypes under the hood ⚡ The truth most people miss: Even with classes… 👉 JavaScript is STILL prototype-based. Classes are just syntactic sugar. 🧠 The real upgrade: Stop thinking: “Which syntax should I use?” Start thinking: “How does inheritance actually work under the hood?” Because once you understand prototypes… You don’t just write code— you understand it. What confused you more in JavaScript—closures, promises, or prototypes? 👇 #JavaScript #WebDevelopment #Programming #Frontend #Coding #SoftwareEngineering

To view or add a comment, sign in

Explore content categories