JavaScript is a prototype-based language, which means that inheritance — the mechanism that allows one object to access properties and methods of another — is implemented through prototypes, not classical classes (though the classsyntax introduced in ES6 provides a cleaner abstraction over it).
Download the Codeflare Mobile for iOS
In simple terms:
Every object in JavaScript can have another object as its prototype, from which it inherits properties and methods.
Every JavaScript object has an internal link to another object called its prototype.
This prototype object is referenced by the special property [[Prototype]] (or __proto__ in most environments).
When you try to access a property on an object and it’s not found, JavaScript looks up the prototype chain — the object’s prototype, then the prototype’s prototype, and so on — until it finds it or reaches the end of the chain (null).
Example:
const person = {
greet() {
console.log("Hello!");
}
};
const student = Object.create(person); // student inherits from person
student.study = function() {
console.log("Studying...");
};
student.greet(); // Output: Hello! (inherited from person)
student.study(); // Output: Studying...
In the example above:
student inherits from person via Object.create().student.greet() is called, JavaScript doesn’t find greet on student, so it looks up to its prototype — person — and finds it there.Here’s how the lookup works internally:
student → person → Object.prototype → null Each arrow represents the prototype link.
When you create a new object, its prototype determines what it can inherit.
Before ES6 class syntax, constructor functions were used to create objects that share methods through their prototype.
Example:
function Car(make, model) {
this.make = make;
this.model = model;
}
// Add method to Car's prototype
Car.prototype.start = function() {
console.log(`${this.make} ${this.model} started.`);
};
const car1 = new Car("Toyota", "Camry");
const car2 = new Car("Honda", "Civic");
car1.start(); // Toyota Camry started.
car2.start(); // Honda Civic started.
Here’s what happens:
new keyword creates a new object.Car.prototype.Car function with this bound to the new object.Car.prototype.The ES6 class syntax is syntactic sugar over the prototype-based model.
Under the hood, it still uses prototypes for inheritance.
Example:
class Animal {
constructor(name) {
this.name = name;
}
speak() {
console.log(`${this.name} makes a sound.`);
}
}
class Dog extends Animal {
speak() {
console.log(`${this.name} barks.`);
}
}
const dog = new Dog("Buddy");
dog.speak(); // Buddy barks.
Here’s what happens:
Dog inherits from Animal via the extends keyword.dog → Dog.prototype → Animal.prototype → Object.prototype → null The Object.create() method allows you to create a new object using an existing one as its prototype — a clean and direct way to set up inheritance without constructors.
Example:
const animal = {
speak() {
console.log(`${this.name} makes a noise.`);
}
};
const cat = Object.create(animal);
cat.name = "Whiskers";
cat.speak(); // Whiskers makes a noise.
Here, cat inherits from animal using Object.create().
prototype is a property of constructor functions and classes — it defines what will be inherited by instances created with new.__proto__ is the actual prototype of an instance object, pointing to the object it inherits from.Example:
function Car() {}
const myCar = new Car();
console.log(Car.prototype === myCar.__proto__); // true | Concept | Description |
|---|---|
| Prototype | The object another object inherits from. |
| Prototype Chain | The linked chain of prototypes used for property lookup. |
| Constructor Function | A function that initializes an object, with shared methods on its prototype. |
| Object.create() | Creates an object using another as its prototype. |
| ES6 Classes | A syntactic sugar for prototype-based inheritance. |
class LivingThing {
breathe() {
console.log("Breathing...");
}
}
class Animal extends LivingThing {
eat() {
console.log("Eating...");
}
}
class Bird extends Animal {
fly() {
console.log("Flying...");
}
}
const eagle = new Bird();
eagle.breathe(); // Breathing...
eagle.eat(); // Eating...
eagle.fly(); // Flying...
JavaScript’s prototype system is powerful, flexible, and dynamic.
Even though class syntax feels familiar to developers from other languages, under the hood it’s still prototypes doing the heavy lifting.
Understanding how prototypes and inheritance work allows you to:
Latest tech news and coding tips.
1. What Is the Golden Ratio? The Golden Ratio, represented by the Greek letter φ (phi), is…
In CSS, combinators define relationships between selectors. Instead of selecting elements individually, combinators allow you to target elements based…
Below is a comprehensive, beginner-friendly, yet deeply detailed guide to Boolean Algebra, complete with definitions, laws,…
Debugging your own code is hard enough — debugging someone else’s code is a whole…
Git is a free, open-source distributed version control system created by Linus Torvalds.It helps developers: Learn how to…
Bubble Sort is one of the simplest sorting algorithms in computer science. Although it’s not…