Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 750
Bren Avatar asked May 31 '14 20:05

Bren


1 Answers

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.

like image 90
Oleg Avatar answered Oct 10 '22 01:10

Oleg