Dr I
Dr I

Reputation: 286

What this is doing to the require statement?

I'm exploring with NodeJS and I've stumble upon the following syntax using the express debug module:

require('debug')(express:server);

What is it? I've never see this form before and doesn't really find a way to google it to retrieve information on it.

Does this form allow a sort of argument passing to the load module statement ?

Upvotes: 0

Views: 41

Answers (1)

Quentin
Quentin

Reputation: 943999

Putting (some, arguments) after something that evaluates as a value does the same thing it does everywhere else. It calls that value as a function.


This is a function expression:

(function () { })

It evaluates as a function and you can call it:

(function () { })()

This is a function that returns a function:

function foo () {
    return function () { };
}

You can call it to get a function:

foo();

and that evaluates as a function so you can call that function immediately:

foo()();

This is a module that returns a function:

module.exports = function () {}

You can require it to get a function:

require("myModule")

And you can call that function immediately:

require("myModule")()

Upvotes: 2

Related Questions