wrapping a callback in javascript
I have two corresponding lists: addresses
and descriptions
.
In JS, I'm using a 3rd-party API method: Foo(addresses, FooCallback);
I wrote a FooCallback
that gets an array of gecodings
. I want to match any gecoding[i]
with description[i]
inside my FooCallback
. Wh开发者_如何学JAVAat is the best design to do so?
You could use a closure to save the state of i
:
var addresses = [...stuff...];
var destinations = [...stuff...];
var i = 3; // maybe from a loop
var FooCallback = (function(destinations, index) {
return function (geocodings, index) {
if (geocodings[index] === destinations[index]) {
// stuff;
}
}
})(destinations, i);
Foo(addresses, FooCallback);
精彩评论