Prototypal inheritance in JavaScript is a way for objects to inherit properties and methods from other objects through their prototype chain. Unlike classical inheritance in some other programming languages, JavaScript uses a prototype-based model.
Here's a breakdown of how prototypal inheritance works:
Objects and Prototypes:
In JavaScript, objects have an internal property called [[Prototype]] or
__proto__, which points to another object.
This relationship forms a chain, known as the prototype chain.
Prototype Chain:
When you access a property or method on an object, JavaScript looks for that property in the object itself.
If it doesn't find the property, it looks in the object's prototype (referenced by
__proto__).
This process continues up the prototype chain until the property is found or until the end of the chain (where the prototype is
null).
Constructor Functions and Prototypes:
Constructor functions are used to create objects with a shared prototype.
When you create an object using a constructor function with the new keyword, the object's
__proto__ is set to the prototype of the constructor function.
Properties and methods defined on the prototype are shared among all instances created from that constructor function.
Here's a simple example:
// Constructor function
function Animal(name) {
this.name = name;
}
// Adding a method to the prototype
Animal.prototype.sayHello = function () {
console.log("Hello, I'm " + this.name);
};
// Creating instances of Animal
var cat = new Animal("Whiskers");
var dog = new Animal("Buddy");
// Both cat and dog can access the sayHello method from the prototype
cat.sayHello(); // Output: Hello, I'm Whiskers
dog.sayHello(); // Output: Hello, I'm Buddy
In this example:
cat and dog are instances of the Animal constructor function.
The sayHello method is defined on the Animal.prototype, making it accessible to both
cat and dog.
This is the fundamental mechanism of prototypal inheritance in JavaScript. It allows objects to share and inherit properties and methods from their prototypes, creating a flexible and efficient way to structure and extend code.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Prototypal inheritance in JavaScript is a way for objects to inherit properties and methods from other objects through their prototype chain. Unlike classical inheritance in some other programming languages, JavaScript uses a prototype-based model.
Here's a breakdown of how prototypal inheritance works:
Objects and Prototypes:
Prototype Chain:
Constructor Functions and Prototypes:
Here's a simple example:
In this example:
This is the fundamental mechanism of prototypal inheritance in JavaScript. It allows objects to share and inherit properties and methods from their prototypes, creating a flexible and efficient way to structure and extend code.