Tuesday, 24 April 2018

When should I store a function into a variable?

For instance, both of the following code blocks do the exact same thing in Node.js:
 var onReq = function(req, res) {
   res.write('Hello');
 };

 http.createServer(onReq).listen(3000);
and
function onReq(req, res) {
   res.write('Hello');
 }

 http.createServer(onReq).listen(3000);
var foo = function(a){ return a * 2; }
var bar = foo(2);

foo = function(a){ return a / 2; }

bar = foo(bar);

No comments:

Post a Comment