0

I learned that javascript doesn't have "interface" concept from Does JavaScript have the interface type (such as Java's 'interface')?

However, I saw the opposite in https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement

The HTMLElement interface represents any HTML element. Some elements directly implement this interface, others implement it via an interface that inherits it.

I was wondering what "interface" and "implementing an interface" mean in the above quote? Help is appreciated!

2 Answers2

1

While JavaScript does not expose the ability to create pure interfaces, it does have the ability to interact with objects that implement interfaces that are provided to the JavaScript runtime via various APIs.

The example you site (the HTMLElement) is an interface that is implemented by the browser itself via the C/C++ language (which does support the creation and implementation of interfaces). The object(s) that that interface is implemented on are provided to the JavaScript runtime in the form of DOM objects for you and I to code against.

Scott Marcus
  • 64,069
  • 6
  • 49
  • 71
0

All the classes (constructors with a prototype) that implement this interface share the same methods however these methods are implemented differently. E.g.:

 class House {
   draw(){ }
 }

 class Tree {
   draw(){}
 }

In this case House and Tree share the same method name, so when writing docs it might be good to summarize their behaviour inside an interface.

Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
  • 1
    So the Point is, the term "Interface" in the context of JavaScript is just a documentational thing and has no conrete reflection in the language itself, like one might be used to from other languages, right? – treeno Dec 19 '17 at 17:34
  • 1
    @treeno exactly. Thats actually a better description than mys ;) – Jonas Wilms Dec 19 '17 at 17:40