What is the reason for the unexpected output of the printed object display prototype chain?

function G () {
   this.x = 1;
   this.y = 2;
}
let obj = new G(); // {x: 1, y: 2}

// adding properties to the prototype of G function
G.prototype.y = 3;
G.prototype.z = 4;
console.log(obj)

After inspecting using Chrome's developer tools, the output is shown below:

https://i.sstatic.net/sazFk.jpg

This output has left me puzzled. Can anyone provide an explanation for this result?

Answer №1

Just a heads up: My confidence level in this response may vary.

__proto__ indicates the object being built (it's linked to the constructor).

[[Prototype]] is the object meant to act as a prototype for the current object.

Answer №2

[[Prototype]] is the internal prototype that every object is created with but is not directly accessible, as shown by this example:

o.prototype.b

This will return undefined.

The deprecated __proto__ starts off empty and only fills up with the values of [[Prototype]] when called, resulting in duplication. Therefore,

o.__proto__.b

will return 3.

To access the prototype, it is recommended to use the getPrototypeOf() static method of Object, like so:

Object.getPrototypeOf(o).b

This also returns 3.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Node js server for world's warm greetings

I have been attempting to utilize Node.js for hosting a web server on a dedicated PC, but unfortunately I am unable to access it from anywhere outside of my local network. After researching online, the general consensus is that all I need to do is enter t ...

What is the best way to prevent jQuery from adding a unique identifier to scripts that are loaded through ajax requests?

Recently, I've been utilizing jquery's ajax functions to fetch a page fragment and showcase it in a section of an existing page - this particular fragment contains html along with references to external javascript files. The flow of the program ...

One text label to display for a MultiLineString using MapLibre GL JS

I am attempting to show text labels for MultiLineString features within a geoJSON file using MapLibre GL JS. By utilizing the symbol-placement: point option, I aim to display the labels across various zoom levels rather than only when extremely close, as w ...

What could be causing the Angular router outlet to not route properly?

Check out this demo showcasing 2 outlets (Defined in app.module.ts): <router-outlet></router-outlet> <router-outlet name="b"></router-outlet> The specified routes are: const routes: Routes = [ { path: 'a', com ...

Horizontal Panning Feature for D3 Horizontal Bar Charts

I am working on a D3 Bar Chart and I would like it to have horizontal panning functionality similar to this example: https://jsfiddle.net/Cayman/vpn8mz4g/1/. However, I am facing an overflow issue on the left side that I need to resolve. Below is the CSV ...

Is it possible to turn off ajax within an iframe?

I'm currently developing a project that allows users to customize their page. What is the best way to prevent ajax functionality without having to disable javascript in an iframe? ...

"Exploring the Effects of Opacity Gradation in

Is there a way to create a face with an opacity gradient in three.js since rgba is not supported and the alpha is not used? I've heard it might be achievable with a ShaderMaterial and custom attributes, but being new to WebGL, I'm still trying t ...

Unable to Combine Header of Slickgrid using the Latest Version of Jquery

I am currently using slickgrid in this sandbox. It functions perfectly with jQuery version 1.8.3, however, when I switch to either jQuery version 1.9.1 or 1.10.1, it stops working altogether. If you want to see for yourself, feel free to adjust the jQuer ...

Issue with HTML5 video not displaying poster image after using .load() method

I am facing an issue with my video implementation where the poster image is not returning after exiting full screen mode. I have a video with overlay content that requests full screen when the user clicks a custom play button. When the user exits full scre ...

Selenium IDE - Automated Calculation Feature

In a form I created, there are fields for weight and height. When I manually enter values in these two fields, the BMI field is automatically calculated. However, when using an IDE to automate the process, the BMI value is not filled in. Could you plea ...

Modify data in firestore collection

Here is the code snippet I'm working with: function saveID(sender_psid, complete){ let data = new Object(); data.TASK= complete; data.ID = sender_psid; db.collection('users').add(data); } Currently, a new document is generated ...

Issue with scrolling feature in div

I am currently facing an issue with scrolling on my website. Visit this site to see the problem. When scrolling down, it causes the "hidden" part of the site to slide up. I want to achieve a similar sliding effect, but it needs to be smooth. I have attempt ...

When working with three.js, I am able to successfully display a .glb 3D model locally. However, when trying to view it on the GitHub server

While working on my personal webpage, I attempted to use three.js to showcase a 3D model. The model appeared successfully on my local server, but failed to display on my web server, resulting in an error message similar to the following: https://i.sstatic. ...

Parse a string and generate an array using regular expressions in JavaScript/Node.js

I'm currently working on coding in JavaScript to extract an array of elements after splitting using a regular expression. var data = "ABCXYZ88"; var regexp = "([A-Z]{3})([A-Z]{3}d{2})"; console.log(data.split(regexp)); The current output is [ &a ...

Discovering the Nearest Object on a Line in Three.js

In my project using Three.js, I am building a simulation of a robot that needs to detect obstacles. More specifically, the robot should be able to determine the distance to the closest obstacle directly in front of it. How can I achieve this using Three. ...

Are there any alternative methods for generating a PDF using PHP?

Hi there, I'm facing an issue with my webpage. I am trying to generate a PDF file asynchronously using jQuery with the following command: $(".btn").click(function(){ $.get('/pl/home/generujPDF', function( data ) { }); In ...

Stop automated web crawlers from crawling through JavaScript links

To enable remote jQuery templating, I have embedded some links in JavaScript. For example: <script type="text/javascript"> var catalog = {}; catalog['key1'] = 'somepath/template_1.html'; catalog['key2'] = 'anotherp ...

visibility:hidden causing issue with hiding empty option in IE dropdown

I am currently working on an Angular 11 application. On one of the pages, there are 2 radio buttons and a dropdown. The desired behavior is that whenever the radio button selection is changed, the dropdown value should be reset to empty. The code provided ...

Retrieve the content of a file and convert it into JSON or GeoJSON format

Can someone help me with my issue? I am struggling to extract content from geojson files and assign it to a valid variable. Here is what I've tested: //this works var obj_valid = {"type": "FeatureCollection","crs": { "type": "name", "properties": { ...

The importance of adding ".$" to an AngularJS filter object

I have been exploring a section of code related to filtering in AngularJS from the documentation website. (cf. http://docs.angularjs.org/api/ng.filter:filter) Specifically, I am curious about the use of .$ appended to the object search, as shown in the fo ...