Bren Bren - 1 year ago 75
Javascript Question

Double parameters with require: var io = require('')(http);

I'm new to node and JS and was working thorough the chat example ( I came across this code in the server:

var app = require('express')();
var http = require('http').Server(app);
var io = require('')(http);

I've looked at other tutorials and never seen double parentheses after require before. What does the
part do? Is it a parameter for require, doest it change the type, or something else?


Answer Source

In JavaScript, function is the First-class citizen. This means that it can be returned by another function.

Consider the following simple example to understand this:

var sum = function(a) {
    return function(b) {
        return a + b;

sum(3)(2);  //5


var func = sum(3);
func(2);   //5

In your example, require('') returns another function, which is called immediately with http object as a parameter.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download