I write two function, which callback will be invoke by the fn:
var callback = function (num) {
return num + 1;
}
var fn = function (callback) {
callback();
}
fn(callback(5));
the firebug tell me: number is not a function ,I know the callback is execute immediately, how can I let it execute in the fn return the num in the fn function, the fn function is only allow the callback parameter
Your example is a not great, seeing as there is no actual need for a second function. But this is what I’d do all the same:
Seeing that a callback is generally called in an object context (DOM elements, when an event is handled etc) You might want to call the callback function, you passed as an argument to be called in the objects’ context.
Closures are a bit daunting and hard to fathom at first, but there are some great videos with Douglas Crockford on youtube on the matter, that really explain their workings well