Prototype and prototype chaining in javascript

One of the key features of JavaScript is its ability to create objects through the use of prototypes.

Hi Everyone,

In this article, we will discuss what are the prototypes and how prototype chaining works in JavaScript.

Prototype One of the key features of JavaScript is its ability to create objects through the use of prototypes.

.

.

.

What are Prototypes in JavaScript?

Prototypes in JavaScript are a method for creating objects based on existing objects. An object's prototype is essentially a blueprint for that object's properties and methods. When an object is created in JavaScript, it inherits all of the properties and methods of its prototype. This allows for more efficient and flexible object creation.

function Person(name) {
  this.name = name;
}

var person1 = new Person("John");

In the above example, we created a Person object using the constructor function. The Person object has a single property called "name". When we create a new instance of the Person object using the "new" keyword, the new object inherits all of the properties and methods of its prototype.

What is Prototype Chaining?

Prototype chaining is a method in JavaScript that allows objects to inherit properties and methods from multiple prototypes. When an object is created in JavaScript, it inherits properties and methods from its prototype. However, if the prototype itself has a prototype, the new object will also inherit properties and methods from that prototype. This creates a chain of prototypes.

To easily understand prototype chaining, let's see example:

function Animal(name) {
  this.name = name;
}

Animal.prototype = {
  getName: function() {
    return this.name;
  }
};

function Dog(name, breed) {
  this.name = name;
  this.breed = breed;
}

Dog.prototype = Object.create(Animal.prototype);
Dog.prototype.constructor = Dog;

Dog.prototype.getBreed = function() {
  return this.breed;
};

var dog1 = new Dog("Fido", "Labrador");

In the above example, we created two objects: Animal and Dog. The Animal object has a single property called "name" and a method called "getName". The Dog object has two properties called "name" and "breed", and a method called "getBreed". The Dog object's prototype is set to the Animal object's prototype using Object.create(). This creates a chain of prototypes that the new Dog object can access. When we create a new instance of the Dog object using the "new" keyword, the new object inherits all of the properties and methods of both the Dog object's prototype and the Animal object's prototype. This allows the new object to access the "getName" method from the Animal object and the "getBreed" method from the Dog object.

Prototypes and prototype chaining are important concepts in JavaScript that allow for more efficient and flexible object creation. Prototypes allow objects to inherit properties and methods from existing objects, while prototype chaining allows objects to inherit properties and methods from multiple prototypes. Understanding these concepts is essential for helping a creating dynamic web application.

THanks fOr ReaDInG !

Did you find this article valuable?

Support Danish Rayeen by becoming a sponsor. Any amount is appreciated!