5

The NodeJS Documentation states:

"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."

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?

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

Source - Anatomy of an HTTP Transaction

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!

  • Possible duplicate of [Multiple inheritance/prototypes in JavaScript](https://stackoverflow.com/questions/9163341/multiple-inheritance-prototypes-in-javascript) – zero298 Feb 14 '18 at 18:36
  • In traditional Object Oriented Oriented Programming like Java it implements class based Inheritance, Classes are extended and Interfaces are implemented. However JS implements Prototypal Inheritance. In JS inheriting means simply adding to the immediate prototype chain of an Object. I've not yet came across Implementing Interfaces in JS and that is why I've raised this question. Sorry, but your comment but was not helpful at all. – Mohammad Daud Ibrahim Feb 14 '18 at 20:28
  • 2
    @WittyHumour There's nothing in the language that enforces interfaces. When the documentation says "Implements the ReadableStream interface" it means that YOU as a human will go and read the documentation of ReadableStream and YOU as a programmer agree to write your code so that when other programmers use your object it will look like a ReadableStream object. It's just an informal agreement between humans. Not a language feature – slebetman Feb 14 '18 at 20:39
  • The request object is an instance of IncomingMessage and it implements the ReadableStream interface. Can you please explain this in context with JavaScript. – Mohammad Daud Ibrahim Feb 14 '18 at 21:10

1 Answers1

7

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?

Well, here it says

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

And what is the stream.Readable?

Well, here a complete "interface" is provided and it consists of multiple members, including read, pipe and others.

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

  • 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 interface contains a method

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

You have following options.

Option 1, a direct implementation

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

Option 2, delegation down the proto chain

// 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

// 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.

Wiktor Zychla
  • 47,367
  • 6
  • 74
  • 106
  • Can you please explain more clearly, in a way a beginner (like me) can understand. – Mohammad Daud Ibrahim Feb 15 '18 at 16:00
  • 1
    @WittyHumour: edited and provided more background, problem is, you still ask for more clarity but you don't narrow what exactly you have problem with understanding. – Wiktor Zychla Feb 15 '18 at 16:45
  • In Class Based Inheritance for example in C# when we say implement Interface, it basically says to apply all the methods in the Interface Right! However in JavaScript with Prototypal Inheritance you said " To implement an interface = Provide members which are expected by these who call you. " – Mohammad Daud Ibrahim Feb 15 '18 at 18:13
  • However in JavaScript with Prototypal Inheritance You explained " To implement an interface = Provide members which are expected by these who call you. ". So the Object creates Objects out of those Interfaces ?? And the Interfaces are Objects ?? PS - Sorry its really confusing. I've programmed in Java but in JS its different (constructor function, no Interfaces). And Node has Class/Interfaces all extending/inheriting. – Mohammad Daud Ibrahim Feb 15 '18 at 18:23
  • 1
    @WittyHumour: In pure JS there is no way to express the interface in a way you are used to, so that the compiler will tell you about possible missing members. The only way in pure JS to express the interface is to write down its members **in the docs** and keep your fingers crossed someone who "implements" it will actually implement all members. It is the **TypeScript** that introduces interfaces checked at the compile time, however the TypeScript compiles down to pure JS . Take a look at the example I created: goo.gl/hUqw7j – Wiktor Zychla Feb 16 '18 at 08:26
  • 1
    @WittyHumour: as you can see, the interface is completely removed during the compilation. The point of this example is that JS devs often use TS to express the metadata (types and interfaces), the whole idea of the Definitely Typed repo is about providing the metadata for many JS frameworks (including node.js etc.) Thus, by saying "interface", people often refer to the TS specs of typed contracts. Just go there https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/node/index.d.ts and search for **ReadableStream**. – Wiktor Zychla Feb 16 '18 at 08:31
  • I still can't get my head around it, guess I'm not much skilled as of now. Anyways Thanks for all your help & I will continue searching/learning and then look up to your explanation for understanding. Best regards! – Mohammad Daud Ibrahim Feb 16 '18 at 19:43
  • 1
    @WittyHumour: no problem. Next time you have specific issues and want people help you, try to be more specific on what exactly you don't get. My impression is that you repeat 4th or 5th time that "you don't get it" but which part exacly, that still remains unclear. – Wiktor Zychla Feb 17 '18 at 12:18
  • It's really a simple question I've explained it few times. Again JS by default does not support interfaces, So how does Node or other technologies implement the concept of Interfaces in JavaScript ? And how it is different from the way other class based inheritance language like Java/C# ?. And your explanation is very complex to understand members? typed contracts? – Mohammad Daud Ibrahim Feb 17 '18 at 16:54
  • 1
    @WittyHumour: Read again my answer starting from "e.g. the docs says [...]" and explain which part of the answer is unclear. – Wiktor Zychla Feb 17 '18 at 21:43
  • This answer as it stands, answers the gist of the question i.e. how js can be used with interfaces. The question itself could be improved as it seems to ask different vague things. – steviesh Jan 19 '19 at 23:38