Prototype inheritance is a key concept in JavaScript where objects inherit properties and methods from other objects rather than from classes as in traditional class-based inheritance models this is achieved through instances.
Key Concepts
Prototypes
Every object in JavaScript has a prototype. Prototypes are basically fallback objects that the object can inherit and a way to find someone’s hand if they are not found on the object itself.
When you access a property or method on an object, JavaScript checks that object directly first. If not found, it then searches for an instance of the object, and continues up the initial chain until a property or method is found or until it reaches the end of the chain (where the original chain is null).
Prototype Chain
A preliminary chain is created by setting one object as a prototype for another object. This provides communication between objects, allowing access to properties and properties of methods.
JavaScript objects can be linked together through their instances, forming a chain that JavaScript uses to resolve inheritance access.
Constructor Functions
Constructor functions in JavaScript are used to create objects with specific attributes and methods. It is often used to start objects that will share the same primitive chain.
When a constructor creates an instance of a new object with the new keyword, the newly created object inherits
properties and methods from the prototype of the constructor function.
prototype Property
Every function in JavaScript has a prototype property, where properties and methods can be added sequentially by instantiating them as constructors to that function.
When an object is created using a constructor function, its prototype is set to the constructor's
prototype property.
// Constructor function
function Person(name, age) {
this.name = name;
this.age = age;
}
// Adding methods to the prototype of Person
Person.prototype.sayHello = function() {
console.log(`Hello, my name is ${this.name}`);
};
// Creating instances of Person
let person1 = new Person('Alice', 30);
let person2 = new Person('Bob', 25);
// Accessing properties and methods
console.log(person1.name); // Output: "Alice"
person2.sayHello(); // Output: "Hello, my name is Bob"
In the example above-
Person is a constructor function that creates instances with
name and age properties.
The sayHello method has been added to Person.prototype, making it available to all instances created by Person.
person1 and person2 inherit properties (name,
age) and methods (sayHello) from Person.prototype.
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.
JavaScript prototypal inheritance
Prototype inheritance is a key concept in JavaScript where objects inherit properties and methods from other objects rather than from classes as in traditional class-based inheritance models this is achieved through instances.
Key Concepts
Prototypes
null).Prototype Chain
Constructor Functions
newkeyword, the newly created object inherits properties and methods from the prototype of the constructor function.prototypePropertyprototypeproperty, where properties and methods can be added sequentially by instantiating them as constructors to that function.prototypeproperty.Also, Read: Prototypal Inheritance vs. Class-Based Inheritance in JavaScript
Example-
In the example above-
Personis a constructor function that creates instances withnameandageproperties.The
sayHellomethod has been added toPerson.prototype, making it available to all instances created byPerson.person1andperson2inherit properties (name,age) and methods (sayHello) fromPerson.prototype.Also, Read: What is event delegation in JavaScript, and why is it useful?