简体   繁体   English

JavaScript中对象和接口之间的区别

[英]Difference between object and interface in JavaScript

I'm currently learning JavaScript using the Mozilla Developer Network as a reference. 我目前正在使用Mozilla开发人员网络作为参考来学习JavaScript。

It confuses me that MDN refers to objects as interfaces. 使我感到困惑的是,MDN将对象称为接口。

For example: Document.createTreeWalker() 例如: Document.createTreeWalker()

At the bottom, there is a link to the TreeWalker Object, but it says: 在底部,有一个指向TreeWalker对象的链接,但是它说:

The interface of the object it creates: TreeWalker 它创建的对象的接口 :TreeWalker

How can TreeWalker be an interface if I can instantiate it? 如果我可以实例化TreeWalker,该如何使其成为接口? And TreeWalker is only an example, almost every object in MDN is referred to as an interface. 而TreeWalker只是一个例子,几乎MDN中的每个对象都称为接口。

I come from a Java background, so the use of interface in this context is not clear to me. 我来自Java背景,因此我不清楚在这种情况下如何使用接口。 Does interface in JavaScript mean something different than in Java? JavaScript中的接口意味着不同于Java中的接口吗?

"interface" arises in this context because that is the term used in standards applicable to the (javascript) object being described on MDN. 在这种情况下出现“接口”,因为这是在MDN上描述的适用于(javascript)对象的标准中使用的术语。

Web standards use an interface definition language or IDL described in Web IDL to describe the behavior of browser objects independent of the language, eg javascript, used to manipulate them. Web标准使用接口定义语言Web IDL中描述的IDL来描述浏览器对象的行为,这些行为独立于用于操纵它们的语言(例如javascript)。 Hence TreeWalker is documented as an "interface" in the Document Object Model (DOM) Level 2 Traversal and Range Specification 因此, TreeWalker文档对象模型(DOM)2级遍历和范围规范中被记录为“接口”

So in a sense MDN is simply using the vocabulary of the standard which defines the interface exposed by the browser object it is describing. 因此,从某种意义上说,MDN只是在使用标准的词汇表,它定义了所描述的浏览器对象公开的接口。 To what extent the subject of the documentation would be described as an "interface" in another language might vary on a case by case basis. 在不同情况下,文档主题将以另一种语言描述为“界面”的程度可能会有所不同。


CF the definition of interface used in web standards. CF Web标准中使用的接口定义。

No, javascript doesn't really have a true interface type or anything. 不,JavaScript实际上没有真正的接口类型或其他任何类型。 The createTreeWalker() function simply uses Object.create to return on object. createTreeWalker()函数仅使用Object.create返回对象。

See also: 也可以看看:

How to implement interface in javascript 如何在JavaScript中实现接口

Does JavaScript have the interface type (such as Java's 'interface')? JavaScript是否具有接口类型(例如Java的“接口”)?

There isn't a true interface built into JavaScript. JavaScript中没有内置真正的接口。 It seems like they are referring to an object that gets inherited from as the interface. 似乎他们指的是作为接口继承自的对象。 In your example Document.createTreeWalker() returns an instance of the TreeWalker object. 在您的示例Document.createTreeWalker()返回TreeWalker对象的实例。

// myTreeWalker is your object. TreeWalker is the interface.
var myTreeWalker = Document.createTreeWalker()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM