Exploring Objects with the Prototype Chain in JavaScript

When exploring an object using a tree structure in the browser console, you can inspect it like this:

console.log(document);

You can navigate through the object, including its prototype and subsequent prototypes. I attempted to replicate this feature but encountered an issue with logging the "prototype of prototype."

My code successfully retrieves the keys within the prototype, but encounters an error when trying to log them (using var for easier testing in the console). Here is a simplified version demonstrating the problem:

var prototype = Object.getPrototypeOf(document);
var prototypeOfPrototype = Object.getPrototypeOf(prototype);

// Accessing the keys works fine

var keys = Object.getOwnPropertyNames(prototypeOfPrototype);

console.log(keys);

var values = keys.map((key) => {
    
    // An error occurs here even though there should be no execution
    
    console.log(prototypeOfPrototype[key]);

});

The errors I'm receiving are as follows:

Chrome:

"Illegal invocation"

Firefox:

"Uncaught TypeError: 'get implementation' called on an object that does not implement interface Document."

These errors aren't very informative to me. If anyone has insights into why this is happening or how to implement this feature without encountering the error, I would greatly appreciate it.

Answer №1

When you try to access a getter on an object where it shouldn't be accessed, such as the prototype instead of the actual instance like document, issues can arise. It would be wise to utilize Object.getOwnPropertyDescriptors rather than Object.getOwnPropertyNames:

var prototype = Object.getPrototypeOf(document);
var prototypeOfPrototype = Object.getPrototypeOf(prototype);
var properties = Object.getOwnPropertyDescriptors(prototypeOfPrototype);

for (const [key, descriptor] of Object.entries(properties)) {
    const value = "value" in descriptor ? descriptor.value : descriptor.get.call(document);
    console.log(key, typeof value);
}

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

Encountering difficulties when attempting to load a module with the "js" extension in a TypeScript environment

When making a GET request with Systemjs, the extension .js is not being added to the URL. These are my TypeScript Classes customer.ts import {Address} from "./Address"; export class Customer { private _customerName: string = ""; public Customer ...

Learn how to utilize a Library such as 'ngx-doc-viewer2' to preview *.docx and *.xlsx files within the application

After 3 days of searching, I finally found a solution to display my *.docx and *.xlxs files in my angular application. The API returns the files as blobs, so my task was to use that blob to show the file rather than just downloading it using window.open(bl ...

Project Polymer, the magic of touch interactions

I'm feeling a bit lost here, struggling to find the information I need. When it comes to attaching a JavaScript function to a mouse up event, my code looks like this: <div id="div1" on-mouseup={{menuMouseUp}}></div> But what about touch ...

Determining the type of <this> in an Object extension method using TypeScript

I am attempting to incorporate a functionality similar to the let scope function found in Kotlin into TypeScript. My current strategy involves using declaration merging with the Object interface. While this approach generally works, I find myself missing ...

I am interested in retrieving document information from Firebase

/* eslint-disable indent */ import React, { useEffect, useState } from 'react'; import { Alert, } from 'react-native'; import firebase from 'firebase'; // import { useIsFocused } from '@react-navigation/native'; im ...

Are the buttons appearing within a pop-up display?

I am having an issue with my Javascript popup buttons. Whenever the popup appears, the buttons that come after the one clicked appear on top of the popup container, and the previous buttons appear behind it. How can I ensure that the popup container displa ...

An error occurred during the bundling process in React Native

I recently started using react-native and encountered a perplexing error that I cannot quite figure out. My goal is to integrate a calendar library into my project: https://github.com/wix/react-native-calendars I have added the necessary dependency for t ...

Script to pop up cancel alert box

There's a dilemma with this code - if you click CANCEL the first time the box pops up, it continues to run. I'm unsure of how to make it redirect to the underlying page when clicked on cancel for the first time. var numero= prompt ("Enter a nu ...

JavaScript - Navigating through JSON object in reverse (from leaf to root) direction

FamilyTree= { "name":"Family", "photo":"images/family.jpg", "members":[ { "name":"Parent", "photo":"images/parent.jpg", "relationships":[ { "name":"Spouse", "photo":"images/spouse.jpg" }, ...

Instructions for sorting an array of objects by Firebase Timestamp

I am looking for a way to order my messages based on timestamp in Firebase v9. In earlier versions, I was able to do this but now I seem to be facing some difficulties. Here is the data structure set up on Firestore: const [messages, setMessages] = useSta ...

Guide: How to change a sibling component's state from another sibling or imported component in REACT

Greetings! I have recently delved into the world of ReactJS and have been experimenting with import and export functions. To give you an idea, my app has a structure consisting of 3 separate files - a parent component and two children components. My questi ...

What is the most effective method for achieving a desired outcome?

Is it a valid approach to get an action result, and if so, how can this be achieved? For instance, if there is a page with a form for creating entities, after successfully creating an entity, the user should be redirected to the entity's detail view. ...

Unable to retrieve information from the database during the http.get request

Hey everyone, I've encountered an issue that I need help with. I'm working on retrieving data from a database using an HTTP call and then returning it to the front end. Here's what I have so far: app.get('/contentHandler/post/frontPage ...

Using regular JavaScript with code inside ng-view in AngularJS involves incorporating event listeners and manipulations that can interact

Just delving into the world of angularJS for the first time. I've set up the routing service, which involves having a div with routing that calls in a template containing HTML code. I also have a range of regular JavaScript functions necessary for the ...

Load images in advance using jQuery to ensure they are cached and retrieve their original sizes before other activities can proceed

When a user clicks on a thumbnail, I aim to display the full-size image in a div. To achieve this, I want to determine the original size of the image based on its source URL before it loads, allowing me to set the appropriate dimensions for the container. ...

What is the best way to adjust the sprite's position in real-time

Look at this cool sprite I discovered! .common-spinner.common-spinner-40x55 { height: 55px; width: 40px; } .common-spinner { background: url("images/loading-sprite.png") no-repeat scroll 100% 100% transparent; } <div class="loading"> ...

What is the best way to retrieve JSON values based on a key using JavaScript, jQuery, or AngularJS?

Here is some JSON data that I need help with: var jsonData = { "title": "Testing", "settings": { "mySettings": false }, "jsonList": ["TestingList"], "testJsonVals": { "Test1": { "name": "name1", ...

Client-Side Isomorphic JS: A Guide to Using HTTP Requests

Seeking advice on data population for isomorphic flux apps. (Using react, alt, iso, and node but principles are transferable) In my flux 'store' (), I need to fetch data from an api: getState() { return { data : makeHttpRequest(url) ...

Avoid refreshing the page upon pressing the back button in AngularJS

Currently, I am working on building a web application that heavily relies on AJAX with AngularJS. One issue I am facing is that when the user clicks the back button on their browser, the requests are being re-made which results in data having to be reloa ...

Upon the initial hover, the data added to the title tag following an Ajax call is not appearing

I am currently working on an ajax request that retrieves information such as username, email, and user_id. Once the ajax call is successful, I use jQuery to append this data to the title tag. The main issue I am facing is that the data is only displayed af ...