Search code examples
javascriptinheritancedominterface-implementation

DOM interfaces: inheritance vs. implementation


On multiple places of MDN like here there are quotes like

Inherits properties from its parent, Node, and implements the ChildNode interface.

What is the difference between inherits and implements here? I am confused from interface implementing interface. What does it mean to be parent interface and implemented interface?

I'd like to map the DOM tree to better understand from which interface comes which property in javascript.


Solution

  • 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
    

    As you can see, a doctype instance has the method defined by ChildNode in the specification, but since javascript does not support multiple inheritance this is not expressible through the type system.

    In other programming languages multiple inheritance or support for mixins in the type system would be used to codify the relationship.

    The chain of concrete prototype objects looks as follows, at least in firefox:

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

    The ChildNode methods seem to be injected into DocumentType.prototype.