Classes vs Prototypes

🎭 Two Syntaxes, One Mechanism

ES6 classes are syntactic sugar over JavaScript's existing prototype-based inheritance. They don't introduce new inheritance model, but provide cleaner syntax.

Transformation: Class → Prototype
ES6 Class Syntax
class Animal {
  constructor(name) {
    this.name = name;
  }
  
  eat() {
    return 'eating';
  }
  
  static isAnimal(obj) {
    return obj instanceof Animal;
  }
}

class Dog extends Animal {
  bark() {
    return 'woof';
  }
}
Equivalent Prototype Code
function Animal(name) {
  this.name = name;
}

Animal.prototype.eat = function() {
  return 'eating';
};

Animal.isAnimal = function(obj) {
  return obj instanceof Animal;
};

function Dog(name) {
  Animal.call(this, name);
}

Dog.prototype = Object.create(
  Animal.prototype
);
Dog.prototype.constructor = Dog;

Dog.prototype.bark = function() {
  return 'woof';
};

Example 1: ES6 Classes - Modern Syntax

JavaScript Editor
✅ Advantages of ES6 Classes
Readability
  • Clean, familiar syntax
  • Less boilerplate
  • Clear inheritance with extends
  • Built-in getters/setters
Features
  • Private fields (ES2022)
  • Static methods and properties
  • Method shorthand syntax
  • Super keyword for parent access
Tooling
  • Better IDE support
  • TypeScript compatibility
  • Modern framework support
  • Better documentation

Example 2: Constructor Functions - Traditional Approach

JavaScript Editor
⚠️ When to Use Constructor Functions
Advantages
  • Maximum browser compatibility
  • More control over prototype chain
  • Closures for true privacy
  • No transpilation needed
  • Familiar to legacy codebases
Disadvantages
  • Verbose syntax
  • Easy to make mistakes
  • No built-in private fields
  • Inheritance is cumbersome
  • Less intuitive for OOP developers

Example 3: Performance Comparison

JavaScript Editor
📊 Performance Results Summary
OperationClassesConstructorsFactory FunctionsWinner
InstantiationFastFastSlowestClasses/Constructors
Method CallsFastestFastMediumClasses
Memory UsageMediumMediumHighestClasses/Constructors
InheritanceOptimizedManualN/AClasses
EncapsulationGood (ES2022+)Best (closures)Best (closures)Constructors/Factories

Example 4: When to Use Which Pattern

JavaScript Editor
Exercise: Convert Between Patterns
JavaScript Editor
💡 Modern JavaScript Recommendations
  • Use ES6 classes for most OOP scenarios (cleaner, better performance)
  • Use constructor functions only for legacy browser support
  • Use factory functions for functional programming patterns
  • Use composition over inheritance when possible
  • Consider TypeScript for better type safety with classes
  • Use private fields (#) for true encapsulation (ES2022+)
  • Benchmark performance for critical code paths
🎯 Interview Questions: Classes vs Prototypes
  1. What happens when you extend a class in JavaScript?
  2. How do private fields (#) work in ES6 classes?
  3. What's the difference between static methods in classes vs constructors?
  4. How does super() work in classes?
  5. Can you achieve true encapsulation in constructor functions?
  6. What are the performance implications of classes vs constructors?
  7. How would you implement multiple inheritance in JavaScript?
  8. What happens if you forget new with a constructor function?
  9. How do getters and setters work in classes?
  10. What are the trade-offs between factory functions and classes?
🔧 Migration Guide: Constructor Functions → Classes
Constructor PatternES6 Class EquivalentNotes
function User() class User Basic conversion
User.prototype.methodMethod inside classNo prototype syntax needed
function Child() Parent.call(this);class Child extends Parent constructor() { super(); }Use extends and super()
Object.create(Parent.prototype)extends keywordAutomatic prototype setup
Closure for privacyPrivate fields (#field)ES2022+ feature
User.staticMethodstatic staticMethod() Inside class declaration
Object.defineProperty getter/setterget prop() / set prop()Syntactic sugar