- Courses
- 💻Programming Languages
- 🌐JavaScript
- Objects and Classes
- Prototypes & Inheritance
On this page
Prototypes and Inheritance
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:
- Avoid Modifying Built-in Prototypes: Never add methods to
Array.prototypeorObject.prototype. This is known as "Prototype Pollution" and can cause significant compatibility issues with libraries and future JavaScript updates. - Use Object.getPrototypeOf(): While
__proto__is widely used, it is considered a legacy feature. For professional production code, useObject.getPrototypeOf(obj)to inspect prototypes andObject.create()to set them. - Property Ownership: Use the
Object.hasOwn()method (or the olderhasOwnProperty()) to determine if a property exists directly on the object or if it is being inherited from the prototype chain.
Found this helpful? Share the knowledge...
... or support this project to keep it alive!
Previous
this Keyword
Next
Classes
-
The Prototype Chain
-
The proto vs. prototype Property
- The proto Property (Internal Link)
- The prototype Property (Constructor Only)
-
Prototypal Inheritance in Practice
- Creating Inheritance Links
-
Shadowing Properties
-
Performance and Memory Efficiency
-
Technical Rules of Inheritance
