Sometimes I see JavaScript that is written with an argument provided that already has a set value or is an object with methods. Take this jQuery example for instance:
$(.selector).children().each(function(i) {
console.log(i);
});
When logging i
, you would get the value of whatever i
is in that iteration when looking at the selectors children in the jQuery each
method.
Take this Node.js example:
http.createServer(function(request, response) {
response.writeHead(200, {Content-Type: text/plain});
response.write(Hello World);
response.end();
}).listen(8888);
You can see here that request
and response
are being passed and they contain their own methods that can be acted on.
To me, this looks like were passing a function to the createServer
function with two arguments that have methods already attached to them.
My question is a multipart one:
- Where do these arguments come from?
- If these are just anon functions, how do they receive arguments that can be acted on like other functions?
- How do I create functions that can take my own arguments like this??
- Does this use the power of closures??