What is prototypal inheritance in JavaScript?
Quick Answer
Every JavaScript object has an internal link to another object — its prototype. When you access a property that isn't on the object, the engine walks up the prototype chain until it finds it or hits null. That's inheritance — objects inherit directly from other objects, not from classes.
In JavaScript objects inherit from other objects, not from classes. Every object has a hidden link to a prototype object, and when you read a property that the object doesn't have, the engine follows that link up the chain until it finds it or runs out. So [].map works because map lives on Array.prototype. The class keyword from ES6 is just a nicer syntax over this — extends wires up the prototype chain and the methods you define go on the prototype, so all instances share one copy.
obj.__proto__(orObject.getPrototypeOf(obj)) is the object's prototype; a function'sprototypeproperty is the object that instances created withnewwill get as their prototype.classsyntax (ES6) is sugar over this —extendssets up the prototype chain, methods go on the prototype.Object.create(proto)makes a new object with a specific prototype.- Methods are shared via the prototype (one copy), not duplicated per instance.