A Simple Guide to Prototype Inheritance in JavaScript (2025)*

 Inheritance and the Prototype Chain in 

JavaScript

JavaScript Prototype Inheritance | Hinancier


Prototypal inheritance is an important feature built in JavaScript programming language. This feature allows objects to access properties of the "template" object and use them as their own.

A prototype can actually be referred to as a template from which other objects can be built. Properties and methods from the prototype are inherited by objects linked to the protorype.

When accessing the properties of an object, they are not only searched within the object but also in the prototype until they are found.

Note, if the properties or methods searched are not found, the search will continue up the prototypes until an end is reached where null will be returned.

This linkage of objects to copy features from each other is called the prototype chain.


Property and Methods Inheritance in the Prototype Chain

In JavaScript, properties and methods are inherited all together. Methods can be passed as properties containing functions since JavaScript does not have the class-based inheritance method.

Example 1

In this example, we are going to create an object named parent and pass it down so that it can be inherited by another object called child.

Creating the objects

Parent and Child objects

Inheriting properties from the parent object

For the child object to inherit properties, we can say:

child.__proto__  = parent

Another way to accomplish this will be through passing the proto as a property in the child object then assigning the parent variable as its value.

child object inheriting parent properties

Now with that set, the child object has inherited the properties and method of the parent object. For example, if you console.log child.greet() it should print 'Hello parent!'.


child object accessing parent properties

Post a Comment

أحدث أقدم