Element without properties

Asked

Viewed 24 times

1

See the following code snippet below:

var element = document.getElementById('myDiv');

console.log('element: ', element);

console.log('has tagName property: ', element.hasOwnProperty('tagName'));
console.log('all properties: ', Object.getOwnPropertyNames(element));

console.log('tagName value: ', element.tagName);
<div id="myDiv"></div>

Note that the element exists, but all attempts to verify the existence of its properties are unsuccessful. But in the end I can usually get the value of the property that, according to the checks, should not exist.

According to the documentation of MDN the function hasOwnProperty() only returns direct properties of the evaluated object. In this case the property tagName should not be returned?

  • 2

    So the property tagName is inherited from Object, then it is not a direct property of the element in question.

  • 1

    As stated by Anderson, the property is not in the object but in the parent object. This way you will list the properties: Object.getOwnPropertyNames(element.__proto__.__proto__)

  • Well, thank you very much for the comments! Perhaps an answer that contemplates a bit of the basic hierarchy of objects in javascript for this specific case (an html element) is enough to end this question.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.