Fuzzyma
Fuzzyma

Reputation: 8484

why document has getElementById method even though it doesnt inherit from Element?

Following the MDN docs, the Element interface holds all methods to search for dom nodes like

MDN also states that Document inherits directly from Node. NOT from Element.

So why does Document hold all these Element methods (and methods from the ParentNode interface)? Is MDN just not up to date with the specs or am I missing something?

Upvotes: 1

Views: 118

Answers (1)

CertainPerformance
CertainPerformance

Reputation: 370979

Both Document.prototype and Element.prototype have getElementsByTagName. One isn't inheriting from the other - they're completely separate functions (unintuitively):

console.log(
  Element.prototype.hasOwnProperty('getElementsByTagName'),
  Document.prototype.hasOwnProperty('getElementsByTagName'),
  Element.prototype.getElementsByTagName === Document.prototype.getElementsByTagName,
  Document.prototype.hasOwnProperty('getElementById'),
  Element.prototype.hasOwnProperty('getElementById'),
);

Element.prototype does not have getElementById.

The ParentNode interface is an abstract specification, not an actual Javascript object that you can examine somewhere. Both Element.prototype and Document.prototype implement it, but they do so by putting ParentNode methods directly on their prototypes. (ParentNode is not at all the same thing as Node)

Upvotes: 3

Related Questions