node.js - Where do the parameters in a javascript callback function come from? -
i understand essence of callback functions in function executed again after being passed parameter function. however, i'm confused variables inside callback function come shown in following node.js example:
router.get('/', function(req, res){ res.render('index', {}); }); how variables req , res populated? example explaining how can call res.render(...) without declaring res myself appreciated.
they come same place come when normal non callback function invoked, @ invocation time.
if have function,
function add (a, b) { return + b } you're fine knowing , b come when invoke add,
add(1,2) and it's same principle callbacks, don't let brain twisted because it's getting invoked later.
at point function pass router.get going invoked, , when does, receive req , res.
let's pretend definition router.get looks this
router.get = function(endpoint, cb){ //do var request = {} var response = {} cb(request, response) // invocation time } in case of example, it's node pass function request , response whenever .get invoked.
Comments
Post a Comment