Prototypes & Inheritance
JavaScript uses prototypal inheritance - objects can inherit properties and methods from other objects via prototype chains, which is fundamentally different from classical inheritance.
🤔 What are Prototypes?
Prototypes are the mechanism by which JavaScript objects inherit features from other objects. Every object has a prototype, and prototypes themselves are objects.
- Prototype Chain - Objects linked to other objects
- Delegation - Property lookup delegated up the chain
- Dynamic - Can modify prototypes at runtime
- ES6 Classes - Syntactic sugar over prototypes
💡 Key Concepts
__proto__- Reference to object's prototype (legacy)Object.getPrototypeOf()- Get prototype (modern)Object.setPrototypeOf()- Set prototypeObject.create()- Create with specific prototypeprototypeproperty - On constructor functions
The Prototype Chain
When accessing a property, JavaScript looks up the prototype chain until it finds the property or reaches null.
📝 Prototype Basics
Understanding prototypes, constructor functions, and property lookup.
Prototype Concepts:
- Constructor Functions - Create objects with
new - prototype property - Shared by all instances
- __proto__ - Link to constructor's prototype
- Property Shadowing - Instance properties override prototype
- Object.create() - Direct prototype assignment
🧬 Prototypal Inheritance
Implementing inheritance using prototypes and comparing with ES6 classes.
Inheritance Patterns:
- Constructor functions with
Object.create() - Call parent constructor with
.call()or.apply() - Reset
constructorproperty - Use
instanceofandisPrototypeOf() - ES6 classes (syntactic sugar over this pattern)
🔧 Built-in Prototypes
Exploring built-in object prototypes and extending them (with caution).
Built-in Prototypes:
Array.prototype- Array methodsString.prototype- String methodsNumber.prototype- Number methodsFunction.prototype- Function methodsObject.prototype- Base for all objects
Extending Built-ins:
- Pros: Add useful utilities
- Cons: Can break libraries, collisions
- Best Practice: Use utility functions instead
- If you must: Check if method exists first
🎯 Practical Example: UI Framework
Building a simple UI framework using prototypal inheritance.
💡 Framework Design Insights:
- Base component with shared functionality on prototype
- Specialized components inherit and extend
- Method overriding with parent calls
- Component hierarchy management
- State management with change detection
- Event system using callbacks
⚡ Advanced Patterns
Advanced prototype patterns for flexible and performant code.
Advanced Techniques:
- Mixins - Multiple inheritance via
Object.assign() - Composition - Combine behaviors without inheritance
- Chain Optimization - Flatten for better performance
- Memoization - Cache expensive computations
- Dynamic Prototypes - Change behavior at runtime
- Private State - Using closures with prototypes
- OLOO - Objects Linking to Other Objects
Next Steps
Now that you understand prototypes, learn about the Event Loop - the mechanism that makes JavaScript's asynchronous programming possible.