JS Guide
HomeQuestionsTopicsCompaniesResources
BookmarksSearch

Built for developers preparing for JavaScript, React & TypeScript interviews.

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeQuestionsjavascript
Next

Learn the concept

Prototypes & Inheritance

javascript
senior
prototypes

Explain JavaScript's prototype chain and how inheritance works.

prototypes
inheritance
prototype-chain
object-oriented
classes
Quick Answer

JavaScript uses prototypal inheritance where objects inherit directly from other objects through a prototype chain. Each object has an internal [[Prototype]] link that forms a chain used for property lookup until null is reached.

Detailed Explanation

Prototype Chain Basics:

  • Every object has an internal [[Prototype]] property (accessible via proto or Object.getPrototypeOf())
  • When accessing a property, JavaScript walks up the prototype chain until found or null
  • Functions have a 'prototype' property used when creating instances with 'new'

Key Concepts:

  1. Object.prototype: Top of most prototype chains (its prototype is null)
  2. Constructor.prototype: Template for instances created with 'new'
  3. proto: Reference to an object's prototype (deprecated, use Object.getPrototypeOf)

Modern Inheritance Patterns:

  • Object.create(): Create object with specific prototype
  • Object.setPrototypeOf(): Change object's prototype (performance cost)
  • ES6 classes: Primarily syntactic sugar over prototypes, with additional features like private fields (#), enforced new calling, non-enumerable methods, and proper builtin extending

Code Examples

Understanding the prototype chainJavaScript
function Animal(name) {
  this.name = name;
}
Animal.prototype.speak = function() {
  console.log(`${this.name} makes a sound`);
};

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

// Set up inheritance
Dog.prototype = Object.create(Animal.prototype);
Dog.prototype.constructor = Dog;

Dog.prototype.bark = function() {
  console.log(`${this.name} barks!`);
};

const rex = new Dog('Rex', 'German Shepherd');

// Prototype chain:
// rex -> Dog.prototype -> Animal.prototype -> Object.prototype -> null

rex.bark();  // 'Rex barks!' (own prototype)
rex.speak(); // 'Rex makes a sound' (inherited)
rex.toString(); // '[object Object]' (from Object.prototype)

Real-World Applications

Use Cases

Component Class Hierarchies

Building UI framework component systems where base components provide shared lifecycle methods and specialized components extend them through prototype inheritance

Plugin Architecture Design

Creating extensible plugin systems where a base plugin class defines the required interface and third-party plugins extend it via the prototype chain

ORM Model Inheritance

Implementing database model hierarchies where a base Model class provides CRUD operations and specific models inherit and customize behavior

Mini Projects

Prototype-Based Event Emitter

intermediate

Build a custom EventEmitter using both prototype functions and ES6 class syntax, implementing on, off, emit, and once methods

Class Mixin System

advanced

Implement a mixin utility that composes multiple behaviors onto a class via prototype manipulation with conflict resolution

Industry Examples

TypeScript Compiler

Uses extensive class hierarchies with prototype-based inheritance for AST node types throughout the compiler

Mongoose

Uses prototype inheritance for Schema and Document classes; custom model methods are added to the document prototype

Resources

MDN - Inheritance and the prototype chain

docs

JavaScript.info - Prototypal inheritance

article

Related Questions

How does the 'this' keyword work in JavaScript?

mid
this
Next
What are Generators in JavaScript and when would you use them?
Next