javascript 在 NodeJS 中实现接口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48793969/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 08:16:54  来源:igfitidea点击:

Implementing Interfaces in NodeJS

javascriptnode.jsinheritanceinstanceprototypal-inheritance

提问by Mohammad Daud Ibrahim

The NodeJS Documentation states:

NodeJS 文档指出:

"The request object is an instance of IncomingMessage. The request object that's passed in to a handler implements the ReadableStream interface"

"So far we haven't touched on the response object at all, which is an instance of ServerResponse, which is a WritableStream."

“请求对象是 IncomingMessage 的一个实例。传入处理程序的请求对象实现了 ReadableStream 接口”

“到目前为止,我们根本没有触及响应对象,它是 ServerResponse 的一个实例,它是一个 WritableStream。”

JS has Prototypal Inheritance so when the documentation says it is an instance it means it adds to the prototype chain, but how it implements an Interface in JS?

JS 有原型继承,所以当文档说它是一个实例时,它意味着它添加到原型链中,但它如何在 JS 中实现接口?

And how is this all chained up and how it works. The Official NodeJS Documentation does not explain.

以及这一切是如何联系起来的以及它是如何工作的。官方 NodeJS 文档没有解释。

Source - Anatomy of an HTTP Transaction

来源 - HTTP 事务剖析

Edit- My Question is not related to multiple Inheritance in JS. It is about how NodeJS modules implements interface which is natively not supported in JS. I beg your Pardon if there is any mistake in my question or lacking in my knowledge. Thanks!

编辑 - 我的问题与 JS 中的多重继承无关。它是关于 NodeJS 模块如何实现 JS 原生不支持的接口。如果我的问题有任何错误或我的知识不足,请原谅。谢谢!

回答by Wiktor Zychla

Interface = Obligation.

接口 = 义务。

To implement an interface = Provide members which are expected by these who call you.

实现一个接口 = 提供那些打电话给你的人所期望的成员。

What the docs say about this particular interface, the ReadableStream?

文档对这个特定的界面是ReadableStream怎么说的,?

Well, hereit says

嗯,这里

All Readable streams implement the interface defined by the stream.Readable class

所有 Readable 流都实现了 stream.Readable 类定义的接口

And what is the stream.Readable?

什么是stream.Readable

Well, herea complete "interface" is provided and it consists of multiple members, including read, pipeand others.

好吧,这里提供了一个完整的“接口”,它由多个成员组成,包括readpipe和其他人。

Technically, in JS there are multiple ways of "implementing" such obligation:

从技术上讲,在 JS 中有多种“实现”这种义务的方式:

  • an easy way is to set an object that already implements the interface in the prototype chain of an object that is supposed to implement the interface. This way all interface members are delegated (by means of the proto chain) to the other object

  • a more direct way is just to provide all necessary members directly by the object that is supposed to implement the interface

  • a hybrid approach where some members are delegated down the proto chain, some members are implemented directly and some members are delegated to other objects by direct invocation

  • 一个简单的方法是在应该实现接口的对象的原型链中设置一个已经实现接口的对象。这样所有的接口成员都被委托(通过原型链)给另一个对象

  • 更直接的方法是直接由应该实现接口的对象提供所有必要的成员

  • 一种混合方法,其中一些成员被委托给原型链,一些成员被直接实现,一些成员通过直接调用被委托给其他对象

Because of the nature of the language, the caller which expect the interface to be implemented will accept all these possibilities.

由于语言的性质,期望实现接口的调用者将接受所有这些可能性。

E.g. The docs says the interfacecontains a method

例如文档说接口包含一个方法

foo(n)
where n is a number 
returns a number

You have following options.

您有以下选择。

Option 1, a direct implementation

选项 1,直接实现

var o = {
    foo: function(n) {
       return 0;
    }
}

Option 2, delegation down the proto chain

选项 2,在原型链下委托

// an object that already implements the interface
// i.e. has function foo(n)
var existing = ....; 

var o = Object.create(existing);

// o has function foo though its proto

Option 3, delegation by invocation

选项 3,调用委托

// an object that already implements the interface
// i.e. has function foo(n)
var existing = ....; 

var o = {
   foo: function(n) {
       return existing(n);
   }
}

All these options can possibly be expressed with construction functions, too.

所有这些选项也可以用构造函数来表达。