Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

205
Visualizações
"Interface" in MDN documentation for JS?

I have come across word "interface" so many times while reading through MDN documentation for JS, but never fully understood what does it refer to.

I know that there is no such thing as 'interface' in JS like in Java, C#, TypeScript and all other similar OOP languages.

As I know so far, an interface is just a collection of properties and methods definitions (not implementations) that a certain class must implement in order to conform to a particular interface, but that is not, once again, case for JS.

Now, let's imagine that MDN says some property of some object, let's say geolocation property of window.navigator returns GeolocationPosition/GeolocationPositionError object. Then we read that these objects are conform to GeolocationPosition/GeolocationPositionError interface(s).

Checking GeolocationPosition/GeolocationPositionError interfaces through console.dir returns that they are functions. So, does it mean that GeolocationPosition/GeolocationPositionError objects returned by accessing geolocation property on window.navigator are just objects created by GeolocationPosition/GeolocationPositionError functions?

So, does term interface on MDN documentation just refers to function, which creates objects with a certain properties and methods just like regular constructor functions?

Then why I can't generate a new GeolocationPosition/GeolocationPositionError object(s) using these GeolocationPosition/GeolocationPositionError functions and new operator?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Interface actually has a well defined meaning in the context of browser web APIs.

An interface in that context is a WebIDL interface. WebIDL is the "interface specification language" on top of which things like HTML and the DOM specifications are built on.

For example window.navigator implements the navigator interface:

[Exposed=Window]
interface Navigator {
  // objects implementing this interface also implement the interfaces given below
};
Navigator includes NavigatorID;
Navigator includes NavigatorLanguage;
Navigator includes NavigatorOnLine;
Navigator includes NavigatorContentUtils;
Navigator includes NavigatorCookies;
Navigator includes NavigatorPlugins;
Navigator includes NavigatorConcurrentHardware;

Interfaces are similar to what you would expect from "OO languages":

IDL fragments are used to describe object oriented systems. In such systems, objects are entities that have identity and which are encapsulations of state and behavior. An interface is a definition (matching interface InterfaceRest) that declares some state and behavior that an object implementing that interface will expose.

An interface is a specification of a set of interface members (matching InterfaceMembers). These are the members that appear between the braces in the interface declaration.

Interfaces in Web IDL describe how objects that implement the interface behave. In bindings for object oriented languages, it is expected that an object that implements a particular IDL interface provides ways to inspect and modify the object’s state and to invoke the behavior described by the interface.

An interface can be defined to inherit from another interface. If the identifier of the interface is followed by a U+003A COLON (":") character and an identifier, then that identifier identifies the inherited interface. An object that implements an interface that inherits from another also implements that inherited interface. The object therefore will also have members that correspond to the interface members from the inherited interface.

All from the WebIDL specification.

As for why you can't generate your own instances of the implementations browser provide for those interfaces?

I suspect GeolocationPositionError for example cannot be created because it is not exposed on window. If allowing that is useful to users you are welcome to open a feature request in the whatwg/html repo.

about 4 years ago · Juan Pablo Isaza Relatório

0

In the MDN docs, the word interface is often being used to talk about APIs (Application Programming Interface). Indeed, the only thing you have to worry about as a developer is the API (not the implementation details behind the API).

Interfaces are only conceptual in JavaScript but they exist in some extending languages like TypeScript.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda