2

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.

Jan Turoň
  • 31,451
  • 23
  • 125
  • 169

1 Answers1

4
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.

the8472
  • 40,999
  • 5
  • 70
  • 122
  • So *implements* here means *"All methods from ChildNode are there, but not by inheritance, but manually added."*? And to the last error. Do I get it right that browsers create object Node that implements Node interface, while ChildNode is just an interface without corresponding object? – Jan Turoň Oct 21 '16 at 14:15
  • More or less. The precise language would vary based on whom you ask. the DOM spec talks about interfaces. Languages implementing DOM have different inheritance concepts. Some languages have multiple inheritance for full classes, some single inheritance + mixins or abstract interfaces. Javascript uses prototype-based inheritance (and ES6 introduces classes built on top of that). So these things would be expressed differently in the C++ or Java type systems for example. – the8472 Oct 21 '16 at 14:27
  • About `Node`, that's the constructor function corresponding to the ancestor prototype. It mostly exists so `instanceof` and other reflection stuff works. – the8472 Oct 21 '16 at 14:29