简体   繁体   English

理解 JavaScript 和 DOM '接口'

[英]Understanding JavaScript and DOM 'Interfaces'

I am playing around with vanilla JavaScript and the DOM.我在玩香草 JavaScript 和 DOM。 While glancing through MDN , I came across the term Interface .在浏览MDN 时,我遇到了术语Interface As per MDN, there are various basic Interface from which other DOM elements inherit.根据 MDN,其他 DOM 元素继承了各种基本接口。 These are HTMLElement , Element , Node and EventTarget .它们是HTMLElementElementNodeEventTarget I understand the concept of a Class in JavaScript, but what does an Interface mean in JavaScript?我理解 JavaScript 中Class的概念,但 JavaScript 中的Interface是什么意思? I tried playing around with the HTMLElement interface, but got errors.我尝试使用HTMLElement界面,但出现错误。

const myElem1 = new HTMLElement('div')
const myElem2 = new HTMLElement('')

Both statements above throw the error Uncaught TypeError: Illegal constructor .上面的两个语句都会抛出错误Uncaught TypeError: Illegal constructor Also, an Interface does not seem to be a Class .此外, Interface似乎不是Class Consider this code:考虑这个代码:

const elmnt = document.getElementById("myDIV");
const height = elmnt.clientHeight

clientHeight is a property on the Interface Element , as per MDN.根据 MDN, clientHeight是 Interface Element上的一个属性。 So if Element was a class, clientHeight should have been on Element.prototype.clientHeight , and not on Element.clientHeight .因此,如果Element是一个类,则clientHeight应该在Element.prototype.clientHeight ,而不是在Element.clientHeight

Is it possible to use these Interfaces in our JavaScript program?是否可以在我们的 JavaScript 程序中使用这些接口? If so, what's the correct way, and what are some use cases?如果是这样,正确的方法是什么,有哪些用例?

Interface is a general term in OO-programming.接口是面向对象编程中的通用术语。

It is like a class in that it defines features that must appear on an object, but you can't use it directly.就像一个类,因为它定义了必须出现在对象上的特征,但您不能直接使用它。 Instead you must use a class which implements it.相反,您必须使用实现它的类。

So you can't create an instance of HTMLElement, but since Div implements HTMLElement and Span implements HTMLElement you can create an instance of Div or Span and either will satisfy a requirement to have an HTMLElement.所以你不能创建 HTMLElement 的实例,但是由于 Div实现了HTMLElement 并且 Span实现了HTMLElement,你可以创建一个 DivSpan 的实例,并且两者都将满足拥有 HTMLElement 的要求。

Interfaces are generally used to avoid problems in multiple inheritance.接口通常用于避免多重继承中的问题。 A class can implement multiple interfaces which have overlapping requirements (say both interfaces require a Dance method) whereas if it extended two classes which had a Dance method you'd need to pick which of the two parent classes to take Dance from an which to ignore).一个类可以实现多个具有重叠需求的接口(比如两个接口都需要一个Dance方法),而如果它扩展了两个具有Dance方法的类,您需要选择两个父类中的哪一个从哪个父类中获取Dance并忽略哪个)。


Note that JavaScript has no Interface language feature (although TypeScript does ).请注意,JavaScript 没有接口语言功能(尽管TypeScript 有)。 The term is just used in the DOM documentation as it is written in general OO terms and not JavaScript specific terms.该术语仅在 DOM 文档中使用,因为它是用一般 OO 术语而不是 JavaScript 特定术语编写的。

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

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