Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • JS Tutorial
  • JS Exercise
  • JS Interview Questions
  • JS Array
  • JS String
  • JS Object
  • JS Operator
  • JS Date
  • JS Error
  • JS Projects
  • JS Set
  • JS Map
  • JS RegExp
  • JS Math
  • JS Number
  • JS Boolean
  • JS Examples
  • JS Free JS Course
  • JS A to Z Guide
  • JS Formatter
Open In App
Next Article:
How does JavaScript .prototype work ?
Next article icon

How does JavaScript .prototype work ?

Last Updated : 24 Mar, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

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.


Next Article
How does JavaScript .prototype work ?

S

surbhikumaridav
Improve
Article Tags :
  • JavaScript
  • Web Technologies
  • Geeks Premier League
  • Geeks-Premier-League-2022
  • JavaScript-Questions

Similar Reads

    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 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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences