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

javascript, node.js, requirejs, socket.io

Solution

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

//...or...

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

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

Problem

I'm new to node and JS and was working thorough the socket.io chat example (http://socket.io/get-started/chat/). I came across this code in the server: ``` var app = require('express')(); var http = require('http').Server(app); var io = require('socket.io')(http); ``` I've looked at other tutorials and never seen double parentheses after require before. What does the `(http)`part do? Is it a parameter for require, doest it change the type, or something else? Thanks!

Original source