Why is it that Object.getOwnPropertyDescriptors does not function on HTMLElements?

Upon traversing the prototype chain, one would expect to find Object.prototype at the bottom (or top?), leading to the assumption that they would function like typical objects. However, when using Object.getOwnPropertyDescriptors, the properties obtained do not match those visible when inspecting the object through console.dir. How can this discrepancy exist?

for (let property of Object.keys(Object.getOwnPropertyDescriptors(document))) {
  console.log(property)
}

Answer №1

Great query! The reason for this lies in the fact that many of the attributes on HTMLElement are actually prototype functions acting as getters and setters.

There's a complex process at work in the DOM that transforms the almost-English

document.body.style = 'background: pink;'
into a visual update on the screen. By using getters and setters, reactivity patterns are supported and unnecessary property redundancies are avoided in numerous HTMLElement elements, helping to optimize memory usage.

For instance:

// Class utilizing prototype getter
class Thing {
  constructor() {
    this.year = new Date().getFullYear()
  }
  get time() {
    return Date.now();
  }
}
console.dir(new Thing());
// > Displays a JSON representation containing `Thing#time` and `Thing#year`

console.log(Object.getOwnPropertyDescriptors(new Thing()));
/* 
 > Outputs an object with *only* the `#year` descriptor 
   because `#time` is a prototype function, not a property
*/

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

Techniques for Grouping an Array of Objects by a Specific Value Key in Angular

I have the following array that needs to be grouped by section_name in HTML. Additionally, I need to first check if the length of the array values for section_name is greater than zero before displaying the results grouped by section_name. I hope my expl ...

AngularJS - one-time execution of view generation from .NET controller

Currently, I have an MVC .NET application integrated with AngularJS. In my route provider configuration, I am utilizing the controllers of MVC to retrieve the views as shown below: .when('/Units', { templateUrl: 'Unit/Units' ...

Tips for creating a login/registration system in a single page application

I've been working on a single-page application with ngRoute for navigation between tabs. Now, I need to incorporate a login and registration feature. After that, users should be able to access all the tabs. I'm unsure about the following: 1) Sho ...

What could be the reason for my button not updating its text using a method?

I attempted to change the inner text of the Edit button to Save after it's clicked using a method, but it doesn't seem to be working. I could really use some help with this. b-button.editbtn.d-flex.flex-row.mb-3(@click="editBlood") ...

Understanding Arrays in Angular JSIn this article, we

I am new to working with Angular JS. Currently, I am populating an array and attempting to showcase its contents on the HTML page using ng-repeat. $scope.groupedMedia = []; // Elements are being added through a for loop. $scope.groupedMedia[year].push(r ...

When attempting to send data to the ServiceStack RESTful service, an error message of 'Access is denied' was received

I created a RESTful service using ServiceStack to send data to a database. It worked perfectly when tested locally. However, after deploying it to a server and running the same jQuery $.ajax call code, I encountered an 'Access is denied' error. I ...

What is the best method for targeting the clicked element using its class name?

I have a scenario where there are multiple elements with the same class name, but I am only interested in changing the class of the element that is clicked. var icon = $('.opener i'); // Need to target the class of the clicked element Functi ...

Having trouble saving post data using ajax in django

I am encountering an issue with saving my partial '_dim' while viewing the main template 'sheet_form_create.html'. Every time I try to post the data for my '_dim' partial, I receive an error. Any assistance would be greatly ap ...

Vue2: when passing a function as a prop, a warning will be triggered indicating that the prop has

As a newcomer to Vue, I've been enjoying working with Single File Components. Before diving into my main project idea, I decided to experiment with some small components to get a better grasp of the concept. One such experiment involved creating a co ...

Develop a sophisticated multi-page application using create-react-app in conjunction with express.js

While I'm comfortable with back-end work, my front-end programming skills have gotten rusty. I used to work a lot with React, but it's been over a year since I last touched it. Recently, I started a project that requires me to refresh my knowledg ...

Attempted to display a Google Map within a lightbox, but encountered difficulties in getting it to

I've copied and pasted my code below. It may contain references to Google or Lightbox being undefined, but I'm unsure how to integrate them into the code provided. My goal is to display a Google map within a Lightbox popup, but so far I have been ...

What is the best way to access an external array using ng-repeat in AngularJS?

My dataset consists of 3 separate arrays. "areas": { "default": [ { "area": "Master Bedroom", "uuid": "986e3f42-1797-49ae-b060-181a33b9", "description": "", "new": [ { "value": "986e3f42-1797-49ae-b060-181a3 ...

Tips for Preventing Repetition of Locations on a Website for Reviewing Places

I am currently working on creating a directory specifically for hospitals, similar to the "Yellow Pages." Can anyone advise on how to avoid duplication of pages for the same hospital? I was considering using longitude and latitude registration, but it se ...

Just starting out with callback functions (using a callback as an argument)(Javascript)

Hello everyone, I'm a beginner here and I have a question about callback functions. Upon reading about them, I felt like I understood the concept. However, when I attempted to implement one in my code, things didn't go as planned. functio ...

What is the best way to implement JavaScript for loading and removing content based on button clicks on a webpage?

I'm in need of a vanilla JavaScript solution (I know JQuery options exist, but I want to stick to vanilla JS for now)? Currently, I am using a simple page as a testing ground for my ongoing project. The page consists of two buttons that load HTML pag ...

What is preventing me from navigating to other pages in my React application?

Recently, I have been experimenting with ReactJS and encountered an issue where I couldn't access my other pages. The code snippet provided below seems to be the root of the problem. I am in the process of developing a multi-page application using Re ...

Svelte remains static and is not experiencing re-rendering

I am currently incorporating the history component in Svelte, where it should display when changes occur in the bids array. const bidsArray = [ { player: 0, bidType: 'pass' }, { player: 1, bidType: 'level', level: '1&apos ...

I am unable to produce sound by clicking on the drum machine

My goal is to develop a basic react drum machine project that I discovered on freecodecamp. The objective is to display 9 drumpads and trigger sound upon clicking. Update: I have successfully rendered all the keys but I am facing issues with the function ...

Guide to connecting two geometric shapes together with the help of three.js

Is there a way to link two spheres together with a line? I'm looking for a solution that mimics two balls connected by a rope or something elastic. Does anyone have any examples to share? ...

What is the best approach to unit testing this React Component?

I have created a component that acts as a wrapper for another component. My question is, how should I approach unit testing for this component? Besides checking the state and method calls to ensure they update the state correctly. Other than rendering pro ...