“界面” JS 的 MDN 文档?
我在阅读 JS 的 MDN 文档时多次遇到“接口”这个词,但从未完全理解它指的是什么。
我知道 JS 中不存在像 Java、C#、TypeScript 和所有其他类似的 OOP 语言那样的“接口”之类的东西。
据我所知,到目前为止,接口只是属性和方法定义(而不是实现)的集合,某个类必须实现这些属性和方法定义才能符合特定的接口,但对于 JS 来说,情况并非如此。
现在,假设 MDN 表示某个对象的某些属性,假设 window.navigator
的 geolocation
属性返回 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
接口实际上在浏览器 Web API 的上下文中具有明确定义的含义。
该上下文中的接口是 WebIDL 接口。 WebIDL 是“界面规范语言”,HTML 和 DOM 规范等内容都是建立在其之上的。
例如
window.navigator
实现 navigator 界面:界面类似于您对“OO 语言”的期望:
全部来自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:Interfaces are similar to what you would expect from "OO languages":
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 onwindow
. If allowing that is useful to users you are welcome to open a feature request in the whatwg/html repo.在 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.