[英]Pass parameters to Javascript callback functions
This is what I have:这就是我所拥有的:
function populateElement(arg1) {
getData(arg1);
}
function getData(query) {
var url = "http://foo" + query + "&callback=processData";
// do stuff
}
function processData(results) {
callbackForGetData(results);
}
function callbackForGetData(result) {
// process data
}
I want to pass two more arguments to function populateElement like so:我想再将两个 arguments 传递给 function populateElement,如下所示:
function populateElement(arg1, arg2, arg3) {
getData(arg1);
}
And have the arg2, arg3 available in callbackForGetData并在 callbackForGetData 中提供 arg2、arg3
function callbackForGetData(result) {
// process data
// use arg2, arg3 here
}
How should I do this?我该怎么做?
To pass more arguments, you can use call
or apply
.要传递更多 arguments,可以使用
call
或apply
。
function log(a, b, c) { console.log('A: ', a, ', b: ', b, ', c: ', c);}
log.call(window, 'first', 'second', 'third');
> A: first , b: second , c: third
log.apply(window, ['first', 'second', 'third'])
> A: first , b: second , c: third
But as Peter has suggested, you have some async stuff going on here and you want to save this on a closure, instead of passing extra arguments.但正如 Peter 所建议的那样,你在这里有一些异步的东西,你想把它保存在一个闭包中,而不是传递额外的 arguments。
Have something like this, save your data in a closure:有这样的东西,将您的数据保存在一个闭包中:
function processData(results) {
// transform data
myModule.callbackForGetData(results);
}
window.myModule = {
populateElement: function(arg1, arg2, arg3) {
this.data = arguments;
this.getData(arg1);
},
getData: function(query) {
var script = document.createElement('script');
script.src = 'http://jsfiddle.net/echo/jsonp/?query=' + query + '&callback=processData';
document.getElementsByTagName('head')[0].appendChild(script)
},
callbackForGetData: function(result) {
// process data
console.log('Args: ', this.data, ', remote result: ', result);
}
}
// test it
myModule.populateElement(1, 2, 3)
> Args: [1, 2, 3] , remote result: Object {query: "1"}
You can pass them on to the callback and access them in the arguments
array您可以将它们传递给回调并在
arguments
数组中访问它们
function getData(result) {
//result === arg1
//arguments[0] === arg1
//arguments[1] === arg2
//arguments[2] === arg3
//and so on
}
getData(arg1, arg2, arg3);
Pass an object as a single paramater:将 object 作为单个参数传递:
function populateElement(arg1, arg2, arg3) {
var params = {"arg1":arg1,"arg2":arg2,"arg3",arg3};
getData(params);
}
function getData(params) {
var query = params.arg1;
var url = "http://foo" + query + "&callback=processData";
// do stuff
}
Your are looking for a closure.您正在寻找关闭。
function getData(query) {
var url = "http://foo" + query + "&callback=processData";
// do stuff
//simulate the callback
processData("some results");
};
function populateElement(arg1, arg2, arg3) {
//Because we define processData here, we have a closure scope that
// lets us see all the arguments to populateElement
window.processData = function(results) {
console.log("ProcessData called with results: " + results +
", arg2: " + arg2 + ", arg3: " + arg3);
};
getData(arg1);
}
//Simulate the starting call
populateElement(4, 5, 6);
Here's a working jsfiddle.这是一个工作的jsfiddle。
Note this only supports a single (1) pending getData call.请注意,这只支持单 (1) 个待处理的 getData 调用。 A more elaborate scheme would be necessary to support multiple concurrent pending getData calls.
需要更精细的方案来支持多个并发挂起的 getData 调用。 Each would need a unique callback closure name, which could be as simple as a trailing numeric serial number.
每个都需要一个唯一的回调闭包名称,它可以像尾随的数字序列号一样简单。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.