Double Parameters With Require: Var Io = Require('socket.io')(http);
Solution 1:
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...varfunc = sum(3);
func(2); //5
In your example, require('socket.io')
returns another function, which is called immediately with http
object as a parameter.
Solution 2:
To expand if you had a library http
and it has a exported module server
.
Lets say we picked apart the line:
var http = require('http').Server(app);
into two lines:
var http = require('http')
Imports the "http" module library as a JSON object into the http variable. This module library has a bunch of modules that you can access now by calling them via the http var.
httpServer = http.Server(app)
This loads the Server module with the express data that you called above (Kind of line a constructor) and puts it into the httpServer var.
The difference above is that instead of the two steps, they are condensing it into one, so that http has the Server module inside it instead of the entire http library. This can be useful if you only want to use that specific part of the http library.
Solution 3:
Nodejs allows you to assign an object/function to the exported module using the statement module.exports = something
. So each one of those statements are importing a library, and then running the function that was assigned to what was exported.
For example, here is the source code for express where they export the createApplication
function.
And here's an article where they go into a bit more detail.
Post a Comment for "Double Parameters With Require: Var Io = Require('socket.io')(http);"