Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 1765

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 62k
Author
Asked: November 25, 20242024-11-25T11:11:07+00:00 2024-11-25T11:11:07+00:00

OOPs in JavaScript – Deeper Into Objects & Dunder Proto

  • 62k

In this post, originally published here we shall dive a bit deeper into nature of objects, and cover the Dunder Proto concept.

This post is part of the Advanced JavaScript series where I try to explain essential advanced JS concepts in a simple manner.

Nature of Objects

Consider the below object –

let details = { name: 'Richard Hendricks', company: 'Pied Piper', }; 
Enter fullscreen mode Exit fullscreen mode

In the above object, if we try to access the property company, it is possible since company is an existing property of the details object.

However, the below snippet would return undefined –

console.log(details.designation); //undefined 
Enter fullscreen mode Exit fullscreen mode

This is because there is no property named designation inside details. This is exactly how we would expect an object to behave.

However, take a look at the example below –

let arr = [1, 2, 4, 5, 7];  console.log(arr.map( () => 21 ); 
Enter fullscreen mode Exit fullscreen mode

The output would be as below –

objects-in-detail

But map() is not a method inside arr. So how is this being computed and where is this coming from?

Dunder Proto __proto__

Inside every object in JavaScript lies a special property called Dunder Proto. The name is coined due to the way this object is represented – __proto__ (accompanied by double underscore on both sides of the word proto).

As we can see in the above image, the object arr (and basically every object you create in JS), has the [[Prototype]]:Array property, inside which lies __proto__. If we expand this [[Prototype]]: Array property in our example, we should be able to see __proto__, which in turn contains a huge list of methods like every, forEach, map, splice, etc.

objects-in-detail

The point to be noted here is that each object we create has a different set of key-value pairs in the __proto__ property.

Whenever we try to call/access a property that does not exist in the defined object, the JS engine goes down the __proto__ chain (or a rabbit 🐇 hole), to search for that property. In the above case, we tried to compute the map() method on an array (which is an object), and it went down the __proto__ chain to look for the same.

This is how the hidden nature of object allows for all array, object, and string methods to be carried out.

Since __proto__ is a special property of an object, it can be accessed as well. Suppose you want to add a new property under __proto__ to the details object above, this is how to do it –

details.__proto__.alertMsg = function () { alert(`Hello Dunder Proto => __proto__`); } 
Enter fullscreen mode Exit fullscreen mode

This function is now added to the __proto__ property as can be seen below –

objects-in-detail


We learnt a hidden nature of objects in JavaScript, and the basics of Dunder Proto. In the next post, we shall learn about why and where Dunder Proto can be used to make our code more efficient.

Until next time! 🙌

javascriptprogrammingwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    ES6 - A beginners guide - Template Literals

    • 0 Answers
  • Author

    Understanding Higher Order Functions in JavaScript.

    • 0 Answers
  • Author

    Build a custom video chat app with Daily and Vue.js

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.