Navaneeth
Navaneeth

Reputation: 2584

can we give a callback with parameters?


    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 :)

Upvotes: 2

Views: 147

Answers (4)

John Dvorak
John Dvorak

Reputation: 27277

The difference between abc(1,2,add) and abc(1,2,add()) is that in the second case, it's not abc that calls add. The consequence is that add executes much sooner than expected and without any arguments.

The usual way to pass extra parameters to a callback is to create an anonymous function that closes over the parameters:

var a=10;
var b=20;
abc(a, b, function(text){
  add(a, b);
  console.log(text); //logs "abc"
});

Upvotes: 0

Konstantin Dinev
Konstantin Dinev

Reputation: 34895

You can call your callback with parameters from your original function and you can also provide the parameters for the callback inside your function call:

function f1 (args, callback, params) {
    callback.apply(this, params); // Params needs to be an array
    callback.apply(this, [args]); // This would call callback with 1 param - args
    callback.call(this, args); // This is the same as the line above
}

function f2 () {
    for (item in arguments) {
        console.log(item);
    }
}

f1('arg', f2, [1, 2, 3]);

If you call your function with a function call inside the parameters then it would immediately evaluate and it would not be executed as a callback.

Upvotes: 1

Matías Fidemraizer
Matías Fidemraizer

Reputation: 64931

  • 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 the add() 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:

function(a, b) {

}

And later you call it this way:

function("hello"); // Where's the second argument??

JavaScript won't complain. Simply b will be undefined.

Upvotes: 5

Junnan Wang
Junnan Wang

Reputation: 657

the difference between add and add(): add is a function and add() is the return value of the function. if you want to get more arguments of the function. use the arguments object every function has an arguments object arguments[0] == 1 arguments[1] == 2 arguments[2] == add.

Upvotes: 1

Related Questions