理解Javascript回调参数 [英] Understanding Javascript callback parameters
问题描述
关于回调的几个问题,使用一些示例代码..
A few questions on callbacks, using some example code..
function uploadPhoto(params){
var win = function(response){
console.log("Success!");
console.log("Response: ", response);
}
var fail = function(error){
console.log("Failed.");
console.log("Error: ", error);
}
var options = {params: params};
var ft = new FileTransfer();
ft.upload(params.img_uri, "localhost:3000/testing"), win, fail, options);
}
在上面的例子中,我们可以使用FileTransfer将图像上传到服务器。如果成功,它将调用 win
函数,如果不成功,则调用 fail
函数。
In the above example, using FileTransfer we can upload an image to a server. If successful, it will call the win
function, and if not successful, call the fail
function.
获胜和失败函数如何接收 回复
或错误
参数?我们没有调用 win(响应)
或失败(错误)
,那么javascript如何在没有我们的情况下接受这些参数在我们的 ft.upload()
行设置它们?
How do the win and fail functions receive the response
or error
parameters? We're not calling win(response)
or fail(error)
, so how does javascript accept these parameters without us setting them on our ft.upload()
line?
更多示例代码:
function win(response){
console.log("Success!");
console.log(response);
};
function fail(error){
console.log("Failed.");
console.log("Error: ", error);
};
function uploadPhoto(params){
var options = {params: params};
var ft = new FileTransfer();
ft.upload(params.img_uri, "localhost:3000/testing"), win, fail, options);
};
在第二个例子中,我发现 win $可以调用c $ c>函数,但是它的'response'参数未定义。这是什么原因?
In this second example, I've found that the win
function can be called, however its 'response' parameter comes up undefined. What is the reason for this?
如果我想让我的 win
函数也接受 params
来自上传功能,所以看起来更像是这样:
What if I wanted to have my win
function to also accept params
from the upload function, so it would look more like this:
var win = function(response, parameters){
console.log("Success with parameters: ", parameters);
console.log("Response: ", response);
}
我的 ft.upload
行需要看起来更像这样:
Would my ft.upload
line need to look more like this:
ft.upload(params.img_uri, "localhost:3000/testing", win(null, params), fail, options);
这里有很多小问题,感谢您的帮助!
Lots of little questions here, Thanks for your help!
推荐答案
获胜和失败函数如何接收
响应
或错误
参数?我们没有调用win(响应)
或失败(错误)
,那么javascript如何在没有我们的情况下接受这些参数在我们的ft.upload()
行设置它们?
How do the win and fail functions receive the
response
orerror
parameters? We're not callingwin(response)
orfail(error)
, so how does javascript accept these parameters without us setting them on ourft.upload()
line?
ft.upload
调用 win
或失败
并为其提供值回复
或错误
。也就是说, ft.upload
可能如下所示:
ft.upload
calls win
or fail
and supplies it with a value for response
or error
. That is, ft.upload
might look something like this:
function upload(arg1, arg2, successCallback, failureCallback, options) {
try {
var response = something(...);
successCallback(response);
} catch(ex) {
failureCallback(ex);
}
}
现在,当你致电 ft.upload(params.img_uri,localhost:3000 / testing,win,fail,options)
, win
传递给上传
作为 successCallback
参数,失败
作为 failureCallback
和 upload
依次使用适当的参数调用其中一个。
Now, when you call ft.upload(params.img_uri, "localhost:3000/testing", win, fail, options)
, win
is passed to upload
as the successCallback
argument and fail
as failureCallback
, and upload
in turn calls one or the other with an appropriate argument.
如果我想让我的
win
函数也从上传中接受params
该怎么办?函数,所以看起来更像是这样:
What if I wanted to have my
win
function to also acceptparams
from the upload function, so it would look more like this:
var win = function(response, parameters){
console.log("Success with parameters: ", parameters);
console.log("Response: ", response);
}
我的 ft.upload
行需要看起来更像这样:
Would my ft.upload
line need to look more like this:
ft.upload(params.img_uri, "localhost:3000/testing"), win(null, params), fail, options);
关闭,但不完全。如果你这样做,你将传递任何 win
和失败
作为参数返回,除非它们返回将返回的函数当 ft.upload
试图调用它们时会出现问题。
Close, but not quite. If you do that, you're passing whatever win
and fail
return as arguments, and unless they return functions that will be a problem when ft.upload
tries to call them.
...这不是巧合,是一个解决方案:让它们返回 ft.upload
可以调用的函数:
...which, not coincidentally, is one solution: Make them return functions that ft.upload
can call:
function makeSuccessCallback(parameters) {
return function successCallback(response) {
console.log("Success with parameters: ", parameters);
console.log("Response: ", response);
}
}
var win = makeSuccessCallback(params);
ft.upload(params.img_uri, "localhost:3000/testing", win, fail, options);
另一种选择是使用 Function.prototype.bind
。您可能已经看到 bind
用于设置函数的此
对象,但我们也可以将其他参数传递给 bind
,返回的函数将使用这些参数作为自己的初始参数。在计算机科学中,这被称为currying。例如:
The other option is to use Function.prototype.bind
. You may have seen bind
used to set a function's this
object, but we can also pass additional arguments to bind
and the returned function will use those arguments as its own, initial arguments. In computer science this is called "currying." For example:
function add(n, m) {
return n + m;
}
var addThree = add.bind(null, 3);
addThree(4); // => 7
var ten = add.bind(null, 4, 6);
ten(); => 10
(由于此功能不关心此
,我们只为第一个参数传递 null
。)
(Since this function doesn't care about this
, we just pass null
for the first argument.)
使用回调我们可以用它来获取一个函数的副本,但其第一个参数是内置的:
With callbacks we can use this to get a "copy" of a function but with its first argument(s) "built in":
function successCallback(parameters, response) {
console.log("Success with parameters: ", parameters);
console.log("Response: ", response);
}
var win = successCallback.bind(null, params);
ft.upload(params.img_uri, "localhost:3000/testing", win, fail, options);
这样,当 ft.upload
来电时 win(响应)
,它实际上会调用 successCallback(params,response)
。
This way, when ft.upload
calls win(response)
, it will actually be calling successCallback(params, response)
.
这篇关于理解Javascript回调参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!