“界面” JS 的 MDN 文档?

发布于 2025-01-09 15:05:04 字数 991 浏览 3 评论 0原文

我在阅读 JS 的 MDN 文档时多次遇到“接口”这个词,但从未完全理解它指的是什么。

我知道 JS 中不存在像 Java、C#、TypeScript 和所有其他类似的 OOP 语言那样的“接口”之类的东西。

据我所知,到目前为止,接口只是属性和方法定义(而不是实现)的集合,某个类必须实现这些属性和方法定义才能符合特定的接口,但对于 JS 来说,情况并非如此。

现在,假设 MDN 表示某个对象的某些属性,假设 window.navigatorgeolocation 属性返回 GeolocationPosition/GeolocationPositionError 对象。然后我们发现这些对象符合 GeolocationPosition/GeolocationPositionError 接口。

通过 console.dir 检查 GeolocationPosition/GeolocationPositionError 接口返回它们是函数。那么,这是否意味着通过访问 window.navigator 上的 geolocation 属性返回的 GeolocationPosition/GeolocationPositionError 对象只是由 GeolocationPosition/ 创建的对象GeolocationPositionError 函数?

那么,MDN 文档中的术语“接口”是否仅指函数,它像常规构造函数一样创建具有特定属性和方法的对象?

那么为什么我无法使用这些 GeolocationPosition/GeolocationPositionError 函数和 new 运算符生成新的 GeolocationPosition/GeolocationPositionError 对象?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

辞慾 2025-01-16 15:05:04

接口实际上在浏览器 Web API 的上下文中具有明确定义的含义。

该上下文中的接口是 WebIDL 接口。 WebIDL 是“界面规范语言”,HTML 和 DOM 规范等内容都是建立在其之上的。

例如 window.navigator 实现 navigator 界面:

[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;

界面类似于您对“OO 语言”的期望:

IDL 片段用于描述面向对象的系统。在这样的系统中,对象是具有身份的实体,并且是状态和行为的封装。接口是一个定义(匹配接口 InterfaceRest),它声明实现该接口的对象将公开的某些状态和行为。

接口是一组接口成员(匹配的 InterfaceMembers)的规范。这些是出现在接口声明中大括号之间的成员。

Web IDL 中的接口描述了实现该接口的对象的行为方式。在面向对象语言的绑定中,期望实现特定 IDL 接口的对象提供检查和修改对象状态以及调用接口描述的行为的方法。

一个接口可以定义为从另一个接口继承。如果接口的标识符后跟 U+003A 冒号(“:”)字符和标识符,则该标识符标识继承的接口。实现从另一个继承的接口的对象也会实现该继承的接口。因此,该对象还将具有与继承接口中的接口成员相对应的成员。

全部来自WebIDL规范。

至于为什么您不能生成自己的浏览器为这些接口提供的实现实例?

例如,我怀疑无法创建 GeolocationPositionError,因为它未在 window 上公开。如果允许这对用户有用,欢迎您在 whatwg/html 存储库中提出功能请求。

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.

梦中的蝴蝶 2025-01-16 15:05:04

在 MDN 文档中,接口一词经常用于谈论 API< /a>(应用程序编程接口)。事实上,作为开发人员,您唯一需要担心的是 API (而不是实现API 背后的详细信息)。

接口在 JavaScript 中只是概念性的,但它们存在于一些扩展语言中,例如 TypeScript。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文