Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript callback function and parameters [duplicate]

I want to something similar to this:

function AjaxService()
{

 this.Remove = function (id, call_back)
 {
    myWebService.Remove(id, CallBack)
 }

 function CallBack(res) {
        call_back(res);
    }  
}

so my calling program will be like this:

var xx  = new AjaxService();
xx.Remove(1,success);

function success(res)
{


}

Also if I want to add more parameters to success function how will I achieve it. Say if I have success function like this:

var xx  = new AjaxService();
//how to call back success function with these parameters
//xx.Remove(1,success(22,33));

function success(res,val1, val2)
{


}

Help will be appreciated.

like image 746
Parminder Avatar asked Jan 04 '10 04:01

Parminder


People also ask

Can callback functions have parameters?

forEach() method as an example, the callback function will always receive the current item, its index, and the array itself as arguments, in that order. You can name the parameters for them anything you want, or even omit them entirely, but the method will always pass them in as arguments.

How do you pass parameters to a call back function?

Passing a function to another function or passing a function inside another function is known as a Callback Function. Syntax: function geekOne(z) { alert(z); } function geekTwo(a, callback) { callback(a); } prevfn(2, newfn); Above is an example of a callback variable in JavaScript function.

Can a callback function return a value in JavaScript?

A callback function can return a value, in other words, but the code that calls the function won't pay attention to the return value. Yes, it makes fun sense to try and return a value from a promise callback.

Can callback be synchronous?

Summary. The callback is a function that's accepted as an argument and executed by another function (the higher-order function). There are 2 kinds of callback functions: synchronous and asynchronous. The synchronous callbacks are executed at the same time as the higher-order function that uses the callback.


2 Answers

Use a closure and a function factory:

function generateSuccess (var1,var2) {
    return function (res) {
        // use res, var1 and var2 in here
    }
}
xx.Remove(1,generateSuccess(val1,val2));

What you're passing here is not the generateSuccess function but the anonymous function returned by generateSuccess that looks like the callback expected by Remove. val1 and val2 are passed into generateSuccess and captured by a closure in the returned anonymous function.

To be more clear, this is what's happening:

function generateSuccess (var1,var2) {
    return function (res) {
        // use res, var1 and var2 in here
    }
}
var success = generateSuccess(val1,val2);
xx.Remove(1,success);

Or if you prefer to do it inline:

xx.Remove(1,(function(var1,var2) {
    return function (res) {
        // this is your success function
    }
})(val1,val2));

not as readable but saves you from naming the factory function. If you're not doing this in a loop then Xinus's solution would also be fine and simpler than my inline version. But be aware that in a loop you need the double closure mechanism to disconnect the variable passed into the callback function from the variable in the current scope.

like image 123
slebetman Avatar answered Oct 18 '22 18:10

slebetman


You can pass it as anonymous function pointer

xx.Remove(1,function(){
                           //function call will go here
                           success(res,val1, val2);
                      });
like image 21
Xinus Avatar answered Oct 18 '22 19:10

Xinus