Explain the concept of prototypal inheritance in JavaScript.
Explain the concept of prototypal inheritance in JavaScript.
387
26-Jun-2024
Updated on 26-Jun-2024
Ashutosh Kumar Verma
26-Jun-2024JavaScript 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?