Reputation: 1882
E.g.
https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement
https://developer.mozilla.org/en-US/docs/Web/API/Window
Are these and others like it simply instances of constructor functions, even though mozilla refers to them as interfaces?
If not what is the difference between an Interface and an instance of a constructor function?
Upvotes: 0
Views: 91
Reputation: 141827
They are DOM interfaces and have little to do with JavaScript, other than that JavaScript is the most common language used to interface with them.
A couple of relevant quotes from w3.org
The Document Object Model, despite its name, . . . , is a language independent way to specify interfaces and objects; . . . it may also be implemented using language-specific bindings like the Java or ECMAScript bindings specified in this document.
And:
The DOM specifies interfaces which may be used to manage XML or HTML documents. It is important to realize that these interfaces are an abstraction - much like "abstract base classes" in C++, they are a means of specifying a way to access and manipulate an application's internal representation of a document. In particular, interfaces do not imply a particular concrete implementation. Each DOM application is free to maintain documents in any convenient representation, as long as the interfaces shown in this specification are supported.
JavaScript doesn't have a notion of interfaces, but it can still have objects that implement DOM interfaces, such as the HTMLAnchorElement which implements the HTMLAnchorElement DOM Interface
Upvotes: 2
Reputation: 65806
The World Wide Web Consortium (W3C) is a standard setting body that set standards for a wide range of languages and technologies that are used on the web. They don't make browsers, but they do set standards that browsers need to follow.
So, they write a specification for an object and this specification includes all the details of how developers need to "interface" with the object and how the object should react to developers interacting with those interfaces.
But, as the term implies in most OOP environments, an interface must be "implemented" and that is the job of the software vendor who makes the browser software. They aren't told exactly how to write the code, but the specification does tell them what the code should do and how users will interact with that code.
A simple example might be that, in the US, we have laws that say that automobiles should have airbags and that those airbags should operate in a particular way, but the government doesn't actually make the airbags and an airbag manufacturer may construct one in any way they wish as long as it meets the specifications outlined.
In the end, JavaScript doesn't have a mechanism for creating interfaces, only objects. But, the browser software vendors implement many different interfaces specified by the W3C (and others) via built-in objects, like document
for example.
When I call document.getElementsByTagName("p")
, I don't know exactly what the underlying code that Chrome uses to gather up all the paragraph elements into an array-like object for me, nor do I care (usually). What I care about is that there is a well-known, standardized object called document
and it has a standardized Application Programming Interface (API) that all modern browsers implement.
Upvotes: 4