创建一个 Javascript 回调函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5111381/
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
Create a Javascript Callback function?
提问by fabian
i was wondering how to implement a callback into this piece of code
我想知道如何在这段代码中实现回调
MyClass.myMethod("sth.", myCallback);
function myCallback() { // do sth };
var MyClass = {
myMethod : function(params, callback) {
// do some stuff
FB.method: 'sth.',
'perms': 'sth.'
'display': 'iframe'
},
function(response) {
if (response.perms != null) {
// How to pass response to callback ?
} else {
// How to pass response to callback ?
}
});
}
}
}
回答by jenming
three ways to achieve "// How to pass response to callback ?" :
三种方式实现“//如何将响应传递给回调?” :
callback(response, otherArg1, otherArg2);callback.call(this, response, otherArg1, otherArg2);callback.apply(this, [response, otherArg1, otherArg2]);
callback(response, otherArg1, otherArg2);callback.call(this, response, otherArg1, otherArg2);callback.apply(this, [response, otherArg1, otherArg2]);
1 is the simplest, 2 is in case you want to control the 'this' variable's value inside your callbackfunction, and 3 is similar to 2, but you can pass a variable number of arguments to callback.
1 是最简单的,2 是为了防止您想this在回调函数中控制 ' ' 变量的值,而 3 与 2 类似,但您可以将可变数量的参数传递给callback.
here is a decent reference: http://odetocode.com/Blogs/scott/archive/2007/07/05/function-apply-and-function-call-in-javascript.aspx
这是一个不错的参考:http: //odetocode.com/Blogs/scott/archive/2007/07/05/function-apply-and-function-call-in-javascript.aspx
回答by Peter C
All you have to do is call the callback function the normal way. In this case you would just do callback(response).
您所要做的就是以正常方式调用回调函数。在这种情况下,您只需执行callback(response).
var MyClass = {
myMethod : function(params, callback) {
// do some stuff
FB.method: { 'sth.',
'perms': 'sth.'
'display': 'iframe'
},
function(response) {
if (response.perms != null) {
// How to pass response to callback ?
// Easy as:
callback(response);
} else {
// How to pass response to callback ?
// Again:
callback(response);
}
});
}
}
回答by Anurag
Just invoke the passed in function.
只需调用传入的函数。
callback(response)
回答by Chris Baker
You're close... just use the callback. In this instance, you can form a closure.
你很接近......只需使用回调。在这种情况下,您可以形成一个闭包。
var MyClass = {
myMethod : function(params, callback) {
// do some stuff
FB.method: 'sth.',
'perms': 'sth.'
'display': 'iframe'
},
function(response) {
if (response.perms != null) {
callback(response);
} else {
// response is null, but you can pass it the same as you did above - if you want to. Probably better to have a generic failure handler
ajaxFailHandler();
}
});
}
回答by Andreas Gohr
I think you can simply call callback(response.perms)there. You could also register it as a
我想你可以简单地打电话给callback(response.perms)那里。您也可以将其注册为
member of your class:
你的班级成员:
MyClass.cb = callback;
and later call it:
然后调用它:
MyClass.cb(response.perms)
回答by fabian
MyClass.myMethod("sth.", myCallback);
var myCallback = function myCallback() { // do sth }
var MyClass = {
myMethod : function(params, callback) {
// do some stuff
FB.method: 'sth.',
'perms': 'sth.'
'display': 'iframe'
},
function(response) {
if (response.perms != null) {
callback();
} else {
callback();
}
});
}
}
回答by Makram Saleh
callback.call(null, response);
回答by jvenema
You've got a reference to a function now. Just invoke it:
你现在得到了一个函数的引用。只需调用它:
callback(response.perms);
回答by SergeS
var callback = function() {
};
Thats it :-)
而已 :-)

