繁体   English   中英

正确的函数回调语法是什么?

[英]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
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM