user9119247
user9119247

Reputation:

What do "interface" and "implementing an interface" mean in the context of Javascript?

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!

Upvotes: 0

Views: 962

Answers (2)

Jonas Wilms
Jonas Wilms

Reputation: 138267

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.

Upvotes: 0

Scott Marcus
Scott Marcus

Reputation: 65806

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.

Upvotes: 1

Related Questions