function abc(arg1,arg2, callback){
console.log(arg1 + arg2) // I am doing something really interesting
if(callback){
callback(arg1+ arg2)
}
}
function add(){
console.log("from call back " + arguments[0])
}
var a =10;
abc(1,2, add)
this works fine, but if we need to send some additional arguments to the call back, what should we do??
Here, Apart from (arg1+ arg2) I need some other arguments to be set from the caller of abc to the callback
And, what is the difference between abc(1,2,add) and abc(1,2,add()) ??
Thanks 🙂
abc(1,2,add)=> Giving the function argument as “function type”. This is like giving a pointer to the function for later invoking it.abc(1,2,add())=> Calling theadd()function and giving its return value as argument.Do you need that the callback support more than an argument? Since JavaScript is a dynamic language, just call the same callback function with more arguments:
callback(1,2,3,4)callback(1,2,3,4,5)callback(1,2,3,4,5,6).JavaScript isn’t strict with function signatures: functions have as many arguments as the caller gives to them.
For example, if you’ve a callback function like this:
And later you call it this way:
JavaScript won’t complain. Simply
bwill be undefined.