How does JavaScript .prototype work ?
Last Updated :
24 Mar, 2022
In JavaScript when adding behaviour to objects, then if creating multiple objects using the constructor (calling the constructor function with the 'new' keyword) then the new keyword is the one that is converting the function call into constructor call and every time a brand new empty object is created which create inconsistency because for each object separate copy of function created. So if create 1,000 objects then 1,000 copies of function get created and this creates memory wastage.
So for this reason prototype comes into this picture.
Prototype: It is a simple way to share behaviour and data between multiple objects. The prototypes are the property of the Object constructor.
Syntax:
Object.prototype
All the objects created in JavaScript are instances of 'Object'. Therefore, all of them share the 'Object.prototype' method. We can also override these properties.
Why do you need prototypes?
Sometimes there is a need to add new properties (or methods) to all the existing objects of a given type. This is only possible by adding the new method to the prototype function.
let's understand How the prototype works?
So for every function, two objects are created one for that and another one for its prototype. Every function has its properties prototype, these properties make it to the prototype of the function, so creating objects from this function by calling it in constructor mode whatever objects created will be the prototype of prototype template that means objects follow the template of prototype whatever properties or behaviour mention in prototype, objects will able to access those properties. It can be more clear using the below diagram.
How Prototype works ?
Function_name.prototype: It gives a prototype of the function. If want to go back from prototype to function there are properties mentioned in Prototype i.e, Constructor in that case Function_name.prototype.constructor take back to function.
Example 1:
JavaScript
// Constructor
function vehicle(numWheels, price) {
this.numWheels = numWheels;
this.price = price;
this.getPrice = () => {
return this.price;
}
}
var vehicle1 = new vehicle(10, 378979);
var vehicle2 = new vehicle(36, 899768);
// function.prototype works
vehicle.prototype
// function.prototype.constructor works
vehicle.prototype.constructor
Output:
Function_name.prototype
Here vehicle.prototype gives a prototype of a vehicle function.
Function.prototype.constructor
Here vehicle.prototype.constructor gives the same function which prototype it is like in this case vehicle functions so it gives that function as output.
Objects inherit properties from the prototype:
So all the objects that are created will have an internal reference to a prototype which means for every function there is one copy and all objects share that copy no need to create separate copy means adding behaviour to function instead of access behaviour from the prototype (example getprice() function is behaviour) so calling it from prototype does not get store in function.
Example 2:
JavaScript
//constructor
function vehicle(numWheels, price) {
this.numWheels = numWheels;
this.price = price;
}
// Adding behaviour to constructor vehicle
vehicle.prototype.getPrice = function () {
return this.price;
}
var vehicle1 = new vehicle(5, 100000);
var vehicle2 = new vehicle(10, 390000);
// Function or constructor
vehicle
// function.prototype works
vehicle.prototype
// function call using object
vehicle1.getPrice();
Output:
function
Here constructor does not contain getprice() function within it because it cannot take space inside the functioning vehicle.
function.prototype
But vehicle function prototype contain getprice() function as its properties.
object
vehicle1 object also have getprice() function within its prototype. so it can access easily getPrice() function.
object.prototype
Here like getprice() is not taking space in vehicle function but objects able to access getprice() using the prototype. Although getprice() is within the prototype of the function.
.prototype more uses: It is also used for adding properties at runtime. so that every object have one common property. like if there are 1000 vehicles and all have the same model number. This lookup works first it goes to find it in the object then it goes to prototype to search this property.
Similar Reads
How does Array.prototype.slice.call work in JavaScript ?
Array.prototype.slice.call()Array.prototype.slice.call() is a method in JavaScript, It is used to convert an array-like or iterable object into a genuine array. It allows you to extract a portion of an array or an array-like object and return a new array. This technique is often used to manipulate a
2 min read
JavaScript Prototype
In JavaScript, everything is an object, including functions, arrays, and strings, which are specialized types of objects. JavaScript follows a prototype-based system, where objects inherit properties and methods from other objects through prototypes. This prototype mechanism plays a key role in how
9 min read
How does memory stacks work in Javascript ?
Introduction: The memory stack is a mechanism in JavaScript that allows us to allocate and free memory. If a programmer wants to store some data, they must first create an empty part of the heap that is called a "stack." Then, they can push and then pop values on the stack. When working with strings
3 min read
How JavaScript Works?
JavaScript is a dynamically typed, cross-platform threaded scripting and programming language, used to put functionality and interactivity at the client side as well as to write logic on the server side of a website. It can display content updates, interactive maps, control multimedia, interactive f
13 min read
JavaScript Date prototype Property
The date.prototype property represents the prototype for the Date constructor. The prototype allows adding new properties, and methods. Below are examples of Date prototype Property. Example: javascript var birthday = new Date('June 21, 2018 16:44:23'); var date1 = birthday.getDate(); var day1 = bir
3 min read
JavaScript Error.prototype.toString() Method
The Error.prototype.toString() method is an inbuilt method in JavaScript that is used to return a string representing the specified Error object. Syntax: e.toString() Parameters: This method does not accept any parameters. Return value: This method returns a string representing the specified Error o
1 min read
How does Query.prototype.j() work in Mongoose ?
The Query.prototype.j() function is used to request acknowledgement that this operation has been persisted to MongoDB's on-disk journal. Syntax: Query.prototype.j() Parameters: This function has one val parameter of boolean type. Return Value: This function returns Query Object.Installing mongoose
2 min read
Prototype Inheritance in JavaScript
Prototype inheritance in JavaScript allows objects to inherit properties and methods from other objects. Each object in JavaScript has an internal link to another object called its prototype. This chain of prototypes forms the prototype chain.When you access a property or method on an object, JavaSc
3 min read
JavaScript Array prototype Constructor
The JavaScript array prototype constructor is used to allow to add new methods and properties to the Array() object. If the method is constructed, then it will be available for every array. When constructing a property, All arrays will be given the property, and its value, as default. Syntax: Array.
2 min read
JavaScript Error.prototype.stack Property
To handle any error which occurs while execution, the methods of error handling are used. In these methods, the type of error where it is raised and reasons are given in a definite order or sequence. Stack Structures are used to store these sequences. In JavaScript, all errors and their details are
3 min read