JavaScriptMed
JavaScript Prototypes — Prototype Chain & Inheritance Explained
Prototypes are JavaScript's mechanism for inheritance. Every object has a hidden [[Prototype]] link to another object. When you access a property, JS looks up the prototype chain until it finds it or reaches null.
7 concepts·4 practice problems·Intermediate level
Key Concepts
- •Every object has a [[Prototype]] (accessible via __proto__ or Object.getPrototypeOf)
- •Property lookup walks up the prototype chain
- •Functions have a .prototype property used for constructor instances
- •Object.create() creates objects with a specific prototype
- •ES6 classes are syntactic sugar over prototypes
Topics Covered
PrototypesJavaScript inheritance mechanismPrototype Chain FundamentalsHow JS objects inherit from other objectsProperty Lookup & ShadowingHow JS finds properties on the chainES6 Classes: Prototype SugarWhat class syntax desugars toHow instanceof WorksChecking prototype chain membershipClassical Inheritance in JavaScriptConstructor function inheritance patternsPrototype Pollution AttacksSecurity vulnerability via prototype manipulation
Practice Problems
Common Mistakes to Avoid
- ×Confusing __proto__ with .prototype
- ×Modifying Object.prototype (affects all objects!)
- ×Not understanding that arrays/functions are also objects with prototypes
Interview Tips
- ✓Draw the prototype chain for a given object
- ✓Explain the difference between __proto__ and .prototype
- ✓Know how ES6 classes relate to prototypes