MDN中的JavaScript接口 [英] JavaScript interfaces in MDN

查看:52
本文介绍了MDN中的JavaScript接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,JavaScript是基于原型(而不是类)的OOP,它并未考虑接口,而是依赖于鸭子输入.

As far as I know JavaScript, being OOP based on prototypes - and not classes - do not contemplates interfaces, but relies instead on ducktyping.

但是,我经常在MDN在线文档中看到一些称为接口"的对象,例如存储在此处":

I can often see however in MDN online documentation some objects described as "interfaces", such as Storage in here:

https://developer.mozilla.org/zh-CN/docs/Web/API/存储

独立的window.Storage存在并且是一个函数,但不是构造函数或工厂,并且如果调用则失败.它没有该页面中描述的成员,而是window.localStorage拥有.页面

Indeed window.Storage exists and is a function, but is not a constructor or a factory and fails if invoked. It has no members like the ones described in that page, as window.localStorage instead has. The page

https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage

将window.Storage指向window.localStorage访问的对象.我了解window.localStorage是本机对象,但是我想了解window.Storage的作用以及MDN为什么将其作为接口"来解决,其含义是:浏览器开发人员是否遵守该规范,而不是严格的界面"像基于类的范式那样?

points window.Storage as the object accessed by window.localStorage. I understand that window.localStorage is a native object, but I would like to understand the role of window.Storage and why it is addressed by MDN as an "interface", in what sense: is it a specification to which browser developers adhere, rather than a strict "interface" as intended in class-based paradigm?

在此先感谢您,对这个奇怪的问题感到抱歉.只是想加深我对ES的理解.

Thank you in advance, sorry for the strange question. Just trying to deepen my comprehension of ES.

推荐答案

这不是具有可实例化的构造函数的类,因此我们不将其称为类.它也不是原型对象.

It's not a class with a constructor that you can instantiate, so we don't call it a class. It's not a prototype object either.

MDN在通用OOP中使用术语接口,不仅限于基于类的继承,还指的是带有方法签名的类型定义.

MDN uses the term interface in the generic OOP meaning, which is not restricted to class-based inheritance but refers to a type definition with method signatures.

但是,在非常特定的上下文中,它也使用术语接口Web接口定义语言的名称,Web存储规范用于定义 Storage作为 interface 的一种.尽管JS是最常见的,但是可以用多种语言(称为绑定")来实现这些WebAPI.WebIDL规范甚至定义了如何在JavaScript中表示这样的接口(("ECMAScript绑定"),特别是使用接口对象与其 .prototype s之间的原型继承(基本上就像使用 class Storage扩展一样……)来实现接口的线性继承.代码>).这意味着 Storage的localStorage实例 Storage.prototype.hasOwnProperty('getItem')会按预期工作.

However, it also uses the term interface in the very specific context of the Web Interface definition language, which the web storage specification uses to define Storage as an interface indeed. These WebAPIs can be implemented in multiple languages (called "bindings"), though JS is most common. The WebIDL spec even defines how such an interface is to be represented in JavaScript (the "ECMAScript binding"), in particular that the linear inheritance of interfaces is implemented using prototype inheritance between interface objects and their .prototypes (basically as if using class Storage extends …). This means localStorage instanceof Storage and Storage.prototype.hasOwnProperty('getItem') work as expected.

这篇关于MDN中的JavaScript接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆