Prototypes and Inheritance
The prototype chain, prototypal inheritance, and how objects link and share behavior.
What you'll learn
- What the prototype chain is and how property lookup works
- Every object has a __proto__ linking to its prototype
- Constructor functions and the prototype property
- Object.create() for explicit prototypal inheritance
- How ES6 classes are syntactic sugar over prototypes
Concept
The Prototype Chain
Every JavaScript object has an internal link to another object called its prototype (accessed via Object.getPrototypeOf() or the __proto__ property). When you access a property that does not exist on the object itself, JavaScript walks up this prototype chain until it finds the property or reaches the end (null). This is how inheritance works in JavaScript — it is delegation, not copying.
Constructor Functions
Before ES6 classes, objects were created with constructor functions. When you call a function with new, JavaScript creates a fresh object, sets its prototype to the function's prototype property, calls the function with this bound to the new object, and returns it. Methods shared by all instances are placed on Constructor.prototype so they are stored once, not copied to every instance.
Object.create()
Object.create(proto) creates a new object whose prototype is explicitly the argument you pass. This is the purest form of prototypal inheritance — you pick the prototype directly instead of going through a constructor. Pass null to create an object with no prototype (a truly empty dictionary).
The Chain in Practice
The chain typically looks like: your object → Constructor.prototype → Object.prototype → null. Methods like hasOwnProperty, toString, and valueOf live on Object.prototype and are available to every object through the chain. Understanding this explains why [] has push (from Array.prototype) and why everything has toString.
Classes Are Sugar
ES6 class syntax does not introduce a new inheritance model — it is syntactic sugar over the same prototype system. class methods are placed on the prototype, extends sets up the prototype chain, and super walks up it. Knowing the underlying prototypes helps you debug, inspect, and understand why certain patterns work the way they do.
Example
// Constructor function with shared prototype methods
function Animal(name) {
this.name = name;
}
Animal.prototype.speak = function () {
return `${this.name} makes a sound`;
};
Animal.prototype.describe = function () {
return `Animal: ${this.name}`;
};
function Dog(name, breed) {
Animal.call(this, name); // call super constructor
this.breed = breed;
}
// Set up inheritance: Dog.prototype inherits from Animal.prototype
Dog.prototype = Object.create(Animal.prototype);
Dog.prototype.constructor = Dog;
Dog.prototype.bark = function () {
return `${this.name} says Woof!`;
};
const rex = new Dog("Rex", "Labrador");
console.log(rex.speak()); // "Rex makes a sound" — from Animal.prototype
console.log(rex.bark()); // "Rex says Woof!" — from Dog.prototype
console.log(rex.describe()); // "Animal: Rex" — inherited
// Inspect the prototype chain
console.log(Object.getPrototypeOf(rex) === Dog.prototype); // true
console.log(rex instanceof Dog); // true
console.log(rex instanceof Animal); // true
// Object.create for a plain prototype link
const proto = { greet() { return "hi"; } };
const obj = Object.create(proto);
console.log(obj.greet()); // "hi" — delegated to proto
Shows a constructor function with prototype methods, manual inheritance setup with Object.create, instanceof checks, the prototype chain inspection, and Object.create for direct prototypal linking.
Try it
- JSON Tree Viewer
Inspect object structures and their inherited properties.
- JSON Formatter
Format objects to see their own enumerable properties.
Common mistakes
The mistake
Assigning methods inside the constructor instead of on the prototype
The fix
Methods defined inside the constructor are recreated for every instance, wasting memory. Place shared methods on Constructor.prototype so all instances share one copy via the prototype chain.
The mistake
Forgetting to reset the constructor property after reassigning prototype
The fix
After Dog.prototype = Object.create(Animal.prototype), the constructor pointer is wrong. Reset it with Dog.prototype.constructor = Dog so instanceof and introspection work correctly.
Related Resources
Related Cheatsheets
- JavaScript Cheatsheet
Prototype chain, constructor patterns, and inheritance.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.