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 prototype
  • Object.create() - Create with specific prototype
  • prototype property - 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.

JavaScript Editor
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.

JavaScript Editor
Inheritance Patterns:
  1. Constructor functions with Object.create()
  2. Call parent constructor with .call() or .apply()
  3. Reset constructor property
  4. Use instanceof and isPrototypeOf()
  5. ES6 classes (syntactic sugar over this pattern)

🔧 Built-in Prototypes

Exploring built-in object prototypes and extending them (with caution).

JavaScript Editor
Built-in Prototypes:
  • Array.prototype - Array methods
  • String.prototype - String methods
  • Number.prototype - Number methods
  • Function.prototype - Function methods
  • Object.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.

JavaScript Editor
💡 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.

JavaScript Editor
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.