Understanding Prototypes and Prototype Chaining in JavaScript

Azeem Mirza
4 min readDec 23, 2022

--

Object proptotype

Have you ever wanted to create multiple objects that share a set of properties and methods, without having to define them separately for each object? Prototypes and prototype chaining in JavaScript can help you do just that. In this article, we’ll dive into the details of how these features work and how you can use them in your own code.

JavaScript is an object-oriented programming language that uses prototypes to implement inheritance. In JavaScript, every object has a prototype, and you can use the prototype to add new properties and methods to the object. This is known as prototype chaining.

Prototypes are objects that are used as templates for creating new objects. When you create a new object, the object is linked to a prototype object, which means that the new object inherits the properties and methods of the prototype object. This allows you to create objects with a shared set of properties and methods, without having to define them separately for each object.

Prototype chaining is the process of using multiple prototypes to create a chain of inheritance. This means that an object can inherit properties and methods from multiple prototypes, rather than just a single prototype.

Here’s an example of how prototype chaining works in JavaScript:

// Create a prototype object with a property and a method
let prototypeA = {
propertyA: 'valueA',
methodA: function() {
console.log('I am method A');
}
};

// Create a second prototype object with a property and a method
let prototypeB = {
propertyB: 'valueB',
methodB: function() {
console.log('I am method B');
}
};

// Set prototypeB as the prototype for prototypeA
Object.setPrototypeOf(prototypeA, prototypeB);

// Create a new object that inherits from prototypeA
let objectC = Object.create(prototypeA);

// objectC has access to both prototypeA's and prototypeB's properties and methods
console.log(objectC.propertyA); // Output: "valueA"
console.log(objectC.propertyB); // Output: "valueB"
objectC.methodA(); // Output: "I am method A"
objectC.methodB(); // Output: "I am method B"

In the example above, we created two prototype objects, prototypeA and prototypeB, each with their own property and method. Then, we set prototypeB as the prototype for prototypeA using the Object.setPrototypeOf() method. This means that prototypeA now inherits from prototypeB.

Next, we created a new object, objectC, using the Object.create() method and passing it prototypeA as the prototype. This means that objectC now inherits from both prototypeA and prototypeB.

As a result, objectC has access to both prototypeA’s and prototypeB’s properties and methods. When we try to access the propertyA and propertyB properties of objectC, we get the values "valueA" and "valueB", respectively. Similarly, when we call the methodA() and methodB() methods of objectC, we get the output "I am method A" and "I am method B", respectively.

Prototype chaining is a powerful feature of JavaScript that allows you to create complex object hierarchies and reuse code in a flexible and efficient way. It’s an important concept to understand if you want to get the most out of JavaScript’s object-oriented features.

In addition to using Object.setPrototypeOf() and Object.create() to create prototype chains, you can also use the Object.prototype.__proto__ property to set the prototype of an object. However, this property is deprecated and should not be used in new code. Instead, you should use `Object.set

Prototype()orObject.create()` as they are more standardized and perform better in modern JavaScript engines.

It’s important to note that prototype chaining is different from class-based inheritance, which is found in languages like Java and C++. In class-based inheritance, inheritance is based on a class hierarchy where a subclass inherits from a superclass. In JavaScript, inheritance is based on the prototype chain, where an object can inherit from multiple prototypes and the prototype can be changed at runtime.

Prototype chaining can also lead to issues with this binding, as the this value is determined at runtime based on how the function is called. To avoid these issues, it's generally recommended to use modern JavaScript features like class and extends for object-oriented programming in JavaScript.

Here’s an example of using the class and extends keywords to implement inheritance in JavaScript:

class SuperClass {
constructor() {
this.superProperty = 'super value';
}

superMethod() {
console.log('I am the super method');
}
}

class SubClass extends SuperClass {
constructor() {
super();
this.subProperty = 'sub value';
}
subMethod() {
console.log('I am the sub method');
}
}

const instance = new SubClass();

console.log(instance.superProperty); // Output: 'super value'
console.log(instance.subProperty); // Output: 'sub value'
instance.superMethod(); // Output: 'I am the super method'
instance.subMethod(); // Output: 'I am the sub method'

In the example above, we defined a SuperClass with a property and a method, and a SubClass that extends the SuperClass. The SubClass has its own property and method, as well as access to the SuperClass's property and method.

When we create an instance of the SubClass using the new keyword and the SubClass constructor, the instance has access to both the SuperClass's and the SubClass's properties and methods.

Using the class and extends keywords makes it easier to implement inheritance in JavaScript and avoids some of the issues that can arise with prototype chaining and this binding.

I hope this helps clarify the concept of prototypes and prototype chaining in JavaScript. If you have any further questions, feel free to ask.

To conclude, prototypes and prototype chaining are important concepts in JavaScript that allow you to create complex object hierarchies and reuse code. Prototypes are objects that act as templates for creating new objects, and prototype chaining is the process of using multiple prototypes to create a chain of inheritance. While prototype chaining can be a powerful tool, it can also lead to issues with this binding and is generally considered less modern than using the class and extends keywords for object-oriented programming in JavaScript. Understanding how prototypes and prototype chaining work is crucial for mastering the object-oriented features of JavaScript.

--

--

Azeem Mirza
Azeem Mirza

No responses yet