Skip to content
    geeksforgeeks
    • Interview Prep
      • DSA
      • Interview Corner
      • Aptitude & Reasoning
      • Practice Coding Problems
      • All Courses
    • Tutorials
      • Python
      • Java
      • ML & Data Science
      • Programming Languages
      • Web Development
      • CS Subjects
      • DevOps
      • Software and Tools
      • School Learning
    • Tracks
      • Languages
        • Python
        • C
        • C++
        • Java
        • Advanced Java
        • SQL
        • JavaScript
        • C#
      • Interview Preparation
        • GfG 160
        • GfG 360
        • System Design
        • Core Subjects
        • Interview Questions
        • Interview Puzzles
        • Aptitude and Reasoning
        • Product Management
        • Computer Organisation and Architecture
      • Data Science
        • Python
        • Data Analytics
        • Complete Data Science
        • Gen AI
        • Agentic AI
      • Dev Skills
        • Full-Stack Web Dev
        • DevOps
        • Software Testing
        • CyberSecurity
        • NextJS
        • Git
      • Tools
        • Computer Fundamentals
        • AI Tools
        • MS Excel & Google Sheets
        • MS Word & Google Docs
      • Maths
        • Maths For Computer Science
        • Engineering Mathematics
        • School Maths
    • JS Tutorial
    • Web Tutorial
    • A to Z Guide
    • Projects
    • OOP
    • DOM
    • Set
    • Map
    • Math
    • Number
    • Boolean
    • Exercise
    • Interview Questions
    Open In App

    Prototypal Inheritance using __proto__ in JavaScript

    Last Updated : 02 Jun, 2020
    Comments
    Improve
    Suggest changes
    15 Likes
    Like
    Report
    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 object. Traditionally, in order to get and set the [[Prototype]] of an object, we use Object.getPrototypeOf and Object.setPrototypeOf. Nowadays, in modern language, it is being set using __proto__. Syntax:
    ChildObject.__proto__ = ParentObject
    Example In the given example, there are two objects 'person' and 'GFGuser'. The object 'GFGuser' inherits the methods and properties of the object 'person' and further uses them. html
    <!DOCTYPE html>
    <html lang="en">
        <head>
            <meta charset="UTF-8" />
            <title>prototype</title>
        </head>
        <body>
            <script>
                // object person
                let person = {
                    talk: true,
                    Canfly() {
                        return "Sorry, Can't fly";
                    },
                };
                // Object GFGuser
                let GFGuser = {
                    CanCode: true,
                    CanCook() {
                        return "Can't say";
                    },
                  
                  //  Inheriting the properties and methods of person
                    __proto__: person, 
                };
    
                // Printing on console
                // Property of person
                console.log("Can a GFG User talk: " + GFGuser.talk); 
              
                // Method of person
                console.log("Can a GFG User fly: " + GFGuser.Canfly()); 
              
                // Property of GFGuser
                console.log("Can a GFG User code: " + GFGuser.CanCode); 
              
                // Method of GFGuser
                console.log("Can a GFG User cook: " + GFGuser.CanCook()); 
            </script>
        </body>
    </html>
    
    Output
    Create Quiz

    H

    hacksight
    Improve

    H

    hacksight
    Improve
    Article Tags :
    • JavaScript
    • Web Technologies
    • JavaScript-Misc

    Explore

      JavaScript Basics

      Introduction to JavaScript

      4 min read

      Variables and Datatypes in JavaScript

      6 min read

      JavaScript Operators

      5 min read

      Control Statements in JavaScript

      4 min read

      Array & String

      JavaScript Arrays

      7 min read

      JavaScript Array Methods

      7 min read

      JavaScript Strings

      5 min read

      JavaScript String Methods

      9 min read

      Function & Object

      Functions in JavaScript

      5 min read

      JavaScript Function Expression

      3 min read

      Function Overloading in JavaScript

      4 min read

      Objects in JavaScript

      4 min read

      JavaScript Object Constructors

      4 min read

      OOP

      Object Oriented Programming in JavaScript

      3 min read

      Classes and Objects in JavaScript

      4 min read

      What Are Access Modifiers In JavaScript ?

      5 min read

      JavaScript Constructor Method

      7 min read

      Asynchronous JavaScript

      Asynchronous JavaScript

      2 min read

      JavaScript Callbacks

      4 min read

      JavaScript Promise

      4 min read

      Event Loop in JavaScript

      4 min read

      Async and Await in JavaScript

      2 min read

      Exception Handling

      Javascript Error and Exceptional Handling

      6 min read

      JavaScript Errors Throw and Try to Catch

      2 min read

      How to create custom errors in JavaScript ?

      2 min read

      JavaScript TypeError - Invalid Array.prototype.sort argument

      1 min read

      DOM

      HTML DOM (Document Object Model)

      8 min read

      How to select DOM Elements in JavaScript ?

      3 min read

      JavaScript Custom Events

      4 min read

      JavaScript addEventListener() with Examples

      9 min read

      Advanced Topics

      Closure in JavaScript

      4 min read

      JavaScript Hoisting

      6 min read

      Scope of Variables in JavaScript

      3 min read

      JavaScript Higher Order Functions

      7 min read

      Debugging in JavaScript

      4 min read
    top_of_element && top_of_screen < bottom_of_element) || (bottom_of_screen > articleRecommendedTop && top_of_screen < articleRecommendedBottom) || (top_of_screen > articleRecommendedBottom)) { if (!isfollowingApiCall) { isfollowingApiCall = true; setTimeout(function(){ if (loginData && loginData.isLoggedIn) { if (loginData.userName !== $('#followAuthor').val()) { is_following(); } else { $('.profileCard-profile-picture').css('background-color', '#E7E7E7'); } } else { $('.follow-btn').removeClass('hideIt'); } }, 3000); } } }); } $(".accordion-header").click(function() { var arrowIcon = $(this).find('.bottom-arrow-icon'); arrowIcon.toggleClass('rotate180'); }); }); window.isReportArticle = false; function report_article(){ if (!loginData || !loginData.isLoggedIn) { const loginModalButton = $('.login-modal-btn') if (loginModalButton.length) { loginModalButton.click(); } return; } if(!window.isReportArticle){ //to add loader $('.report-loader').addClass('spinner'); jQuery('#report_modal_content').load(gfgSiteUrl+'wp-content/themes/iconic-one/report-modal.php', { PRACTICE_API_URL: practiceAPIURL, PRACTICE_URL:practiceURL },function(responseTxt, statusTxt, xhr){ if(statusTxt == "error"){ alert("Error: " + xhr.status + ": " + xhr.statusText); } }); }else{ window.scrollTo({ top: 0, behavior: 'smooth' }); $("#report_modal_content").show(); } } function closeShareModal() { const shareOption = document.querySelector('[data-gfg-action="share-article"]'); shareOption.classList.remove("hover_share_menu"); let shareModal = document.querySelector(".hover__share-modal-container"); shareModal && shareModal.remove(); } function openShareModal() { closeShareModal(); // Remove existing modal if any let shareModal = document.querySelector(".three_dot_dropdown_share"); shareModal.appendChild(Object.assign(document.createElement("div"), { className: "hover__share-modal-container" })); document.querySelector(".hover__share-modal-container").append( Object.assign(document.createElement('div'), { className: "share__modal" }), ); document.querySelector(".share__modal").append(Object.assign(document.createElement('h1'), { className: "share__modal-heading" }, { textContent: "Share to" })); const socialOptions = ["LinkedIn", "WhatsApp","Twitter", "Copy Link"]; socialOptions.forEach((socialOption) => { const socialContainer = Object.assign(document.createElement('div'), { className: "social__container" }); const icon = Object.assign(document.createElement("div"), { className: `share__icon share__${socialOption.split(" ").join("")}-icon` }); const socialText = Object.assign(document.createElement("span"), { className: "share__option-text" }, { textContent: `${socialOption}` }); const shareLink = (socialOption === "Copy Link") ? Object.assign(document.createElement('div'), { role: "button", className: "link-container CopyLink" }) : Object.assign(document.createElement('a'), { className: "link-container" }); if (socialOption === "LinkedIn") { shareLink.setAttribute('href', `https://www.linkedin.com/sharing/share-offsite/?url=${window.location.href}`); shareLink.setAttribute('target', '_blank'); } if (socialOption === "WhatsApp") { shareLink.setAttribute('href', `https://api.whatsapp.com/send?text=${window.location.href}`); shareLink.setAttribute('target', "_blank"); } if (socialOption === "Twitter") { shareLink.setAttribute('href', `https://twitter.com/intent/tweet?url=${window.location.href}`); shareLink.setAttribute('target', "_blank"); } shareLink.append(icon, socialText); socialContainer.append(shareLink); document.querySelector(".share__modal").appendChild(socialContainer); //adding copy url functionality if(socialOption === "Copy Link") { shareLink.addEventListener("click", function() { var tempInput = document.createElement("input"); tempInput.value = window.location.href; document.body.appendChild(tempInput); tempInput.select(); tempInput.setSelectionRange(0, 99999); // For mobile devices document.execCommand('copy'); document.body.removeChild(tempInput); this.querySelector(".share__option-text").textContent = "Copied" }) } }); // document.querySelector(".hover__share-modal-container").addEventListener("mouseover", () => document.querySelector('[data-gfg-action="share-article"]').classList.add("hover_share_menu")); } function toggleLikeElementVisibility(selector, show) { document.querySelector(`.${selector}`).style.display = show ? "block" : "none"; } function closeKebabMenu(){ document.getElementById("myDropdown").classList.toggle("show"); }
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
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Campus Training Program
  • Explore
  • POTD
  • Job-A-Thon
  • Blogs
  • Nation Skill Up
  • Tutorials
  • Programming Languages
  • DSA
  • Web Technology
  • AI, ML & Data Science
  • DevOps
  • CS Core Subjects
  • Interview Preparation
  • Software and Tools
  • Courses
  • ML and Data Science
  • DSA and Placements
  • Web Development
  • Programming Languages
  • DevOps & Cloud
  • GATE
  • Trending Technologies
  • Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
  • Preparation Corner
  • Interview Corner
  • Aptitude
  • Puzzles
  • GfG 160
  • System Design
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
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