开发者

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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜