What are prototype in JavaScript

Prototype property

 

Inside the constructor function, we generally only bind the properties we want the object to have, since the properties will mostly be unique between the different instances we create.

As for the methods - we put them on the prototype object - because the methods are most often the same between instances and it would be a waste of resources to duplicate those methods on every instance we create.

 

All JavaScript objects inherit properties and methods from the prototype.

 

function book(first, page, number, code) {
 this.firstName = first;
 this.pageName = page;
 this.number = agnumbere;
 this.code = code;
}
const book1 = new book("English", "Doe", 50, "blue");
const obook2 = new book("Urdu", "Rally", 48, "green");

Tags:

Share:

Related posts