Javascript Javascript回调函数和参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1997531/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript callback function and parameters
提问by Parminder
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.
帮助将不胜感激。
回答by slebetman
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 generateSuccessfunction but the anonymous function returned by generateSuccessthat looks like the callback expected by Remove. val1and val2are passed into generateSuccessand captured by a closure in the returned anonymous function.
你在这里传递的不是generateSuccess函数,而是它返回的匿名函数,generateSuccess它看起来像Remove. val1并被val2传递到generateSuccess返回的匿名函数中的闭包中并被其捕获。
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.
不那么可读,但可以避免命名工厂函数。如果您不是在循环中执行此操作,那么Xinus 的解决方案也将比我的内联版本更好更简单。但请注意,在循环中,您需要双重闭包机制将传递给回调函数的变量与当前作用域中的变量断开连接。
回答by Xinus
You can pass it as anonymous function pointer
您可以将其作为匿名函数指针传递
xx.Remove(1,function(){
//function call will go here
success(res,val1, val2);
});
回答by jrharshath
one way to do this:
一种方法:
function AjaxService {
var args_to_cb = [];
this.Remove = function (id, call_back, args_to_callback_as_array) {
if( args_to_callback_as_array!=undefined )
args_to_cb = args_to_callback_as_array;
else
args_to_cb = [];
myWebService.Remove(id, CallBack)
}
function CallBack(res) {
setTimeout( function(){ call_back(res, args_to_cb); }, 0 );
}
}
So you can use it like this:
所以你可以像这样使用它:
var service = new AjaxService();
service.Remove(1,success, [22,33]);
function success(res,val1, val2)
{
alert("result = "+res);
alert("values are "+val1+" and "+val2);
}
I usually have the callback execute using a setTimeout. This way, your callback will execute when it gets the time to do so. Your code will continue to execute meanwhile, e.g:
我通常使用setTimeout. 这样,您的回调将在有时间执行时执行。您的代码将同时继续执行,例如:
var service = new AjaxService();
service.remove(1, function(){ alert('done'); }); // alert#1
alert('called service.remove'); // alert#2
Your callback will execute after alert#2.
您的回调将在警报#2 之后执行。
Of course, in case of your application, it will happen so automatically since the ajax callback itself is asynchronous. So in your application, you had better not do this.
当然,对于您的应用程序,它会自动发生,因为 ajax 回调本身是异步的。所以在你的应用中,你最好不要这样做。
Cheers!
jrh
干杯!
jrh

