DOM接口:继承与实现

DOM interfaces: inheritance vs. implementation

本文关键字:实现 继承 接口 DOM      更新时间:2023-09-26

在MDN的很多地方,比如这里,都有像

这样的引号

从父节点Node继承属性,实现 ChildNode接口。

继承实现有什么区别?我对接口实现接口感到困惑。父接口实现接口是什么意思?

我想映射DOM树,以更好地理解从哪个接口来的属性在javascript

document.doctype instanceof DocumentType // true
document.doctype instanceof Node // true
Object.getPrototypeOf(document.doctype) == DocumentType.prototype // true
typeof document.doctype["remove"] // "function"
document.doctype instanceof ChildNode // ReferenceError: ChildNode is not defined

可以看到,doctype实例具有规范中ChildNode定义的方法,但由于javascript不支持多重继承,因此无法通过类型系统表达。

在其他编程语言中,可以使用多重继承或类型系统中对混合的支持来编写关系。

具体原型对象的链看起来如下,至少在firefox中是这样的:

document.doctype ->
   DocumentType.prototype ->
   Node.prototype ->
   EventTarget.prototype ->
   Object.prototype ->
   null

ChildNode的方法似乎被注入到DocumentType.prototype