正确的函数回调语法是什么?
What is the correct function callback syntax?
在 javascript 中,当函数至少有一个参数时,我该如何编写对函数的调用和回调?
使用回调时,我的理解如下:
var result = foo();
变成
foo(function(result) {
// code that depends on 'result'
});
但是,如果foo
带参数,那么对foo
的调用应该怎么写呢?
是否符合以下内容:
var result = foo(testdata);
变成
foo(function(result), testdata {
// code that depends on 'result'
});
或
foo(function(result, testdata){
// code that depends on 'result'
});
我不确定语法是否正确。有人可以给我指点资源或帮助我使用正确的语法吗?
将它们作为 2 个不同的参数传递,例如
foo(testdata, function(result){
//do things with result
});
那么 foo 定义为
function foo(testdata, callback){
//call callback when result is ready
}
在 javascript 中,当函数至少有一个参数时,我该如何编写对函数的调用和回调?
使用回调时,我的理解如下:
var result = foo();
变成
foo(function(result) {
// code that depends on 'result'
});
但是,如果foo
带参数,那么对foo
的调用应该怎么写呢?
是否符合以下内容:
var result = foo(testdata);
变成
foo(function(result), testdata {
// code that depends on 'result'
});
或
foo(function(result, testdata){
// code that depends on 'result'
});
我不确定语法是否正确。有人可以给我指点资源或帮助我使用正确的语法吗?
将它们作为 2 个不同的参数传递,例如
foo(testdata, function(result){
//do things with result
});
那么 foo 定义为
function foo(testdata, callback){
//call callback when result is ready
}