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!

4
  • Possible duplicate of Multiple inheritance/prototypes in JavaScript Commented Feb 14, 2018 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. Commented Feb 14, 2018 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 Commented Feb 14, 2018 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. Commented Feb 14, 2018 at 21:10

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

11 Comments

@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.
@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
@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 github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/… and search for ReadableStream.
@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.
@WittyHumour: Read again my answer starting from "e.g. the docs says [...]" and explain which part of the answer is unclear.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.