On this page

Prototypes and Inheritance

Enrico Hochkoeppler Profile

By Enrico Hochkoeppler

Web Developer & TutoriaLibre Founder

In JavaScript, inheritance is the mechanism that allows one object to access properties and methods from another object. Unlike "classical" inheritance found in languages like Java or C++, which use classes as blueprints, JavaScript uses Prototypal Inheritance. Every object in JavaScript has a built-in property that points to another object, called its prototype.

The Prototype Chain​

When you attempt to access a property or method on an object, JavaScript first looks at the object itself. If the property is not found, the engine follows a link to the object's prototype and searches there. This continues up a "chain" of objects until the property is found or the end of the chain—null—is reached.

This behavior explains why an empty object can still use methods like .toString(). Even if you didn't define it, the method exists higher up on the Object.prototype, which is the ultimate parent of almost all objects in the language.

The proto vs. prototype Property​

Understanding the distinction between these two properties is essential for mastering the language's internal architecture.

The proto Property (Internal Link)​

Every object has an internal property (often accessed via __proto__ in older environments or Object.getPrototypeOf() in modern code) that points to its prototype. This is the actual link used during the lookup process in the prototype chain.

The prototype Property (Constructor Only)​

Only functions (specifically constructor functions) have a .prototype property. This is not the prototype of the function itself. Instead, it is the object that will be assigned as the __proto__ to any new instances created using that function with the new keyword.


function Animal(name) {

  this.name = name;

}



// Adding a method to the prototype

Animal.prototype.eat = function () {

  console.log(this.name + " is eating.");

};



const dog = new Animal("Rex");



dog.eat(); // Output: Rex is eating.

console.log(dog.__proto__ === Animal.prototype); // true

Prototypal Inheritance in Practice​

Inheritance allows us to create specialized objects based on more general ones. By linking prototypes, we can ensure that child objects "inherit" the functionality of parent objects without duplicating code in memory.

Creating Inheritance Links​

In modern JavaScript, while we often use the class syntax, the underlying engine uses Object.create(). This method creates a new object and allows you to specify exactly which object should serve as its prototype.


const vehicle = {

  drive: function () {

    console.log("Moving forward...");

  },

};



const car = Object.create(vehicle);

car.wheels = 4;



car.drive(); // Inherited from vehicle

console.log(car.wheels); // Found on the car object itself

Shadowing Properties​

If an object has a property with the same name as a property on its prototype, the object's own property takes precedence. This is called shadowing. The JavaScript engine finds the property on the local object first and stops the search, "shadowing" the version on the prototype.


const parent = {

  hairColor: "Brown",

};



const child = Object.create(parent);

child.hairColor = "Blonde"; // Shadows the parent property



console.log(child.hairColor); // Output: Blonde

Performance and Memory Efficiency​

The primary advantage of prototypal inheritance is memory efficiency. If you have 1,000 instances of a User object, you do not want to create 1,000 copies of a login function. By placing the login function on the User.prototype, all 1,000 instances share a single reference to that one function.

Technical Rules of Inheritance​

To maintain a predictable and performant architecture, observe these constraints:

Found this helpful? Share the knowledge...

... or support this project to keep it alive!

Buy Me a Coffee

Previous

this Keyword

Next

Classes