Javascript 如何将回调作为参数传递给另一个函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6466031/
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
How to pass a callback as a parameter into another function
提问by Wee Kiat
I'm new to ajax and callback functions, please forgive me if i get the concepts all wrong.
我是 ajax 和回调函数的新手,如果我的概念都错了,请原谅我。
Problem:Could i send a callbackfunctionas a parameter to another function that will execute the callback?
问题:我可以将回调函数作为参数发送给另一个将执行回调的函数吗?
function firstFunction(){
//some code
//a callback function is written for $.post() to execute
secondFunction("var1","var2",callbackfunction);
}
function secondFunction(var1, var2, callbackfunction) {
params={}
if (event != null) params = event + '&' + $(form).serialize();
// $.post() will execute the callback function
$.post(form.action,params, callbackfunction);
}
回答by T.J. Crowder
Yup. Function references are just like any other object reference, you can pass them around to your heart's content.
是的。函数引用就像任何其他对象引用一样,您可以将它们传递给您的心。
Here's a more concrete example:
这是一个更具体的例子:
function foo() {
console.log("Hello from foo!");
}
function caller(f) {
// Call the given function
f();
}
function indirectCaller(f) {
// Call `caller`, who will in turn call `f`
caller(f);
}
// Do it
indirectCaller(foo); // alerts "Hello from foo!"
You can also pass in arguments for foo
:
您还可以传递以下参数foo
:
function foo(a, b) {
console.log(a + " + " + b + " = " + (a + b));
}
function caller(f, v1, v2) {
// Call the given function
f(v1, v2);
}
function indirectCaller(f, v1, v2) {
// Call `caller`, who will in turn call `f`
caller(f, v1, v2);
}
// Do it
indirectCaller(foo, 1, 2); // alerts "1 + 2 = 3"
回答by Brad
Also, could be simple as:
此外,可以简单为:
if( typeof foo == "function" )
foo();
回答by ninjagecko
If you google for javascript callback function example
you will get Getting a better understanding of callback functions in JavaScript
如果你用谷歌搜索,javascript callback function example
你会更好地理解 JavaScript 中的回调函数
This is how to do a callback function:
这是如何执行回调函数:
function f() {
alert('f was called!');
}
function callFunction(func) {
func();
}
callFunction(f);
回答by Nicola Peluchetti
Yes of course, function are objects and can be passed, but of course you must declare it:
是的,当然,函数是对象,可以传递,但当然你必须声明它:
function firstFunction(){
//some code
var callbackfunction = function(data){
//do something with the data returned from the ajax request
}
//a callback function is written for $.post() to execute
secondFunction("var1","var2",callbackfunction);
}
an interesting thing is that your callback function has also access to every variable you might have declared inside firstFunction() (variables in javascript have local scope).
有趣的是,您的回调函数还可以访问您可能在 firstFunction() 中声明的每个变量(javascript 中的变量具有局部作用域)。
回答by nothing-special-here
Example for CoffeeScript
:
示例CoffeeScript
:
test = (str, callback) ->
data = "Input values"
$.ajax
type: "post"
url: "http://www.mydomain.com/ajaxscript"
data: data
success: callback
test (data, textStatus, xhr) ->
alert data + "\t" + textStatus
回答by Imranmadbar
You can use JavaScript CallBak like this:
您可以像这样使用 JavaScript CallBak:
var a;
function function1(callback) {
console.log("First comeplete");
a = "Some value";
callback();
}
function function2(){
console.log("Second comeplete:", a);
}
function1(function2);
Or Java Script Promise:
或 Java 脚本承诺:
let promise = new Promise(function(resolve, reject) {
// do function1 job
let a = "Your assign value"
resolve(a);
});
promise.then(
function(a) {
// do function2 job with function1 return value;
console.log("Second comeplete:", a);
},
function(error) {
console.log("Error found");
});