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:
Prototype Inheritance in JavaScript
Next article icon

Prototype Inheritance in JavaScript

Last Updated : 06 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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, JavaScript first checks the object itself. If the property or method isn’t found, it moves up the prototype chain until it finds the property or reaches the end of the chain (null).

JavaScript
const parent = {   	greet: function () {     	console.log("Hello from the parent object!");   	} };  const child = Object.create(parent);  child.sayHi = function () {   	console.log("Hi from the child object!"); };  child.greet(); child.sayHi(); 

Output
Hello from the parent object! Hi from the child object! 

Prototype Chain

The prototype chain is the mechanism that JavaScript uses to resolve properties and methods. If an object doesn’t have a requested property, the JavaScript engine searches up the prototype chain.

JavaScript
function Animal(name) {   	this.name = name; }  Animal.prototype.speak = function () {   	console.log(`${this.name} makes a sound.`); };  const dog = new Animal("Buddy");  console.log(dog.name); dog.speak(); 

Output
Buddy Buddy makes a sound. 

In the above pictorial representation, we have taken an example to illustrate the Prototype Inheritance between a rabbit and another create prototype object which is an animal. We will set the rabbit's prototype object as an animal prototype object wherein we will store all the values of rabbit for a purpose that if in the case in while rabbit properties are missing then JavaScript will automatically take it from animal prototype object.

Now that you have understood a brief detailed description of Prototype inheritance let us see and understand Prototype Inheritance with following approaches -

  • Using __proto__
  • Using Object.setPrototypeOf() method

1. Using __proto__

In this approach, we will use __proto__, which is the special name for the internal and hidden prototype called [[Prototype]]. We will store all the properties of the rabbit in the animal prototype object and thereafter we may access it whenever it is required. This __proto__ is a bit old as well as an outdated approach that exists for some historical reasons associated with JavaScript.

JavaScript
let animal = {     animalEats: true, };  let rabbit = {     rabbitJumps: true, };  // Sets rabbit.[[Prototype]] = animal rabbit.__proto__ = animal; console.log(rabbit.animalEats); console.log(rabbit.rabbitJumps); 

Output
true true

2. Using Object.setPrototypeOf() Method

In this approach, we will use the new JavaScript methods to implement JavaScript Prototype Inheritance, Here we will use Object.setPrototypeOf() method takes two parameters first one is the object which is to have its prototype set and the second one is the object's new prototype. Thereafter we have declared two objects and using those two objects, we will set one of the objects as the prototype object for another object.

JavaScript
let rabbit = {     rabbitJumps: true, }; let animal = {     animalEats: true, }; Object.setPrototypeOf(rabbit, animal); console.log(rabbit.animalEats); console.log(rabbit.rabbitJumps); 

Output
true true

Next Article
Prototype Inheritance in JavaScript

A

amansingla
Improve
Article Tags :
  • JavaScript
  • Web Technologies
  • javascript-object
  • JavaScript-Questions

Similar Reads

    Prototypal Inheritance using __proto__ in JavaScript
    Every object with its methods and properties contains an internal and hidden property known as [[Prototype]]. The Prototypal Inheritance is a feature in javascript used to add methods and properties in objects. It is a method by which an object can inherit the properties and methods of another objec
    2 min read
    JavaScript Inheritance
    Inheritance is a fundamental concept in object-oriented programming (OOP) that allows one class or object to derive properties and behaviours from another. In JavaScript, inheritance is like a parent-child relationship, where objects, functions, or classes can inherit properties and methods from oth
    6 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
    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
    How does JavaScript .prototype work ?
    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 crea
    4 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