在MDN文档之后,Element接口保存了搜索像这样的dom节点的所有方法
- getElementById
- getElementsByTagName
MDN还声明Document直接从Node继承NOT来自Element。
那么,为什么Document持有所有这些Element方法(以及ParentNode接口中的方法(呢?MDN只是没有跟上规格,还是我错过了什么?
Document.prototype
和Element.prototype
都有getElementsByTagName
。其中一个不是从另一个继承的——它们是完全独立的函数(无意中(:
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
不而具有getElementById
。
ParentNode
接口是抽象规范,而不是您可以在某个地方检查的实际Javascript对象。Element.prototype
和Document.prototype
都实现了它,但它们是通过将ParentNode方法直接放在原型上来实现的。(ParentNode
与Node完全不同(