2

I was reading this SO question about using EventTarget, and was confused by this comment

EventTarget is just an interface, not a constructor. Also, you cannot inherit from native DOM structures

I thought an interface was simply what W3C called javascript "classes".
Also, if I try to instantiate a new object from the DOM Node function in chrome dev tools console,

var myNode = new Node();

It returns an error

Uncaught TypeError: Illegal constructor

I know what in interface is in classical OOP, but my question is what is the difference between constructors and interfaces such as Node and EventTarget in javascript?

Community
  • 1
  • 1
the_velour_fog
  • 2,094
  • 4
  • 17
  • 29

1 Answers1

4

DOM interfaces are defined outside of JavaScript, because the DOM can be manipulated in multiple languages (there was once a time when VBScript and Tcl could be used in browsers besides JavaScript), that's why the W3C's DOM specifications are language-agnostic, the DOM doesn't inherently have anything to do with JavaScript.

In cross-platform / cross-language APIs, interfaces are defined in a language like IDL (Interface Description Language), here's the IDL that defines EventTarget (as used by Webkit): https://github.com/adobe/webkit/blob/master/Source/WebCore/dom/EventTarget.idl

JavaScript itself has no concept of interfaces (the same concept as C#/Java interface) as it is a prototype language, it uses Duck-typing in lieu of interfaces with static typing,

JavaScript "classes" are a new feature in ECMAScript 6 (the next major revision of the specification JavaScript is based on), however it can be considered syntactic sugar to simplify the creation of instances from the same prototype constructor and they too are distinct from this interface concept.

In summary:

API interface

  • A specification for a platform or library to implement
  • Defined in IDL or an IDL-like language (e.g. MIDL)
  • Language-independent / Language-agnostic

Language interface

  • Is a feature of a language (interface in Java or C#, __interface in Microsoft Visual C++, protocol in Swift and ObjectiveC)
  • JavaScript has no concept in its language as it a Prototype-paradigm language (and so uses Duck-typing instead)
  • IDL objects can often be represented 1:1 in a language's interface feature, assuming the language supports it
Dai
  • 141,631
  • 28
  • 261
  • 374
  • thanks that makes sense, so basically `Node` and `EventTarget` are "API interfaces" and not language interfaces (they cant be per the javascript language) they can only be implemented by the interpreter itself, and are not available for developers to use directly? – the_velour_fog Sep 09 '15 at 06:42
  • @user4668401 Not exactly. You **could** define your own JavaScript objects which also implement `EventTarget` (by having the `addEventListener`, `removeEventListener`, and `dispatchEvent` member functions) which then satisfy the requirements to pass them into any other function which also expects an `EventTarget` (just as though `EventTarget` *was* a language `interface` object). – Dai Sep 09 '15 at 06:44
  • However these IDL interfaces are generally the concern of JavaScript engine authors (specifically, DOM implementors) rather than programmers who target them. – Dai Sep 09 '15 at 06:44
  • BTW, "ECMAScript 6" aka "ECMAScript 2015" or "ECMA-262 ed 6" is the **current** standard, not the next one. The next is planned to be "ECMAScript 2016", but we'll have to wait and see… ;-) – RobG Sep 09 '15 at 07:02