将带有 javascript 闭包的参数附加到匿名函数中的默认参数

发布于 2024-09-28 11:02:00 字数 595 浏览 3 评论 0原文

我想向 Google geocoder API 调用添加一些额外的参数,因为我在循环中运行它,但不确定如何将闭包参数附加到其匿名函数,该函数已经具有通过调用传入的默认参数API。

例如:

for(var i = 0; i < 5; i++) {
     geocoder.geocode({'address': address}, function(results, status) {
         // Geocoder stuff here
     });
}

我希望能够在传递的 geocoder.geocode() 匿名函数中使用 i 的值,但是如果我在第 4 行使用 }(i)); 进行闭包这将替换第一个会破坏地理编码器的参数。

有没有办法可以使用闭包,或者将 i 的值传递到匿名函数中?

实际上我想做的是:

geocoder.geocode({'address': address}, function(results, status, i) {
    alert(i); // 0, 1, 2, 3, 4
}(i));

但是工作:-)

I want to add some extra parameters to the Google geocoder API call as I'm running it in a loop, but am not sure how to append closure parameters to their anonymous function that already has default parameters that are passed in by the call to the API.

For example:

for(var i = 0; i < 5; i++) {
     geocoder.geocode({'address': address}, function(results, status) {
         // Geocoder stuff here
     });
}

I want to be able to use the value of i in the passed geocoder.geocode() anonymous function, but if I had a closure using }(i)); on line 4 for example that would replace the first parameter which would break the geocoder.

Is there a way I can use closures, or pass the value of i into the anonymous function at all?

Effectively what I want to do is:

geocoder.geocode({'address': address}, function(results, status, i) {
    alert(i); // 0, 1, 2, 3, 4
}(i));

but working :-)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

放飞的风筝 2024-10-05 11:02:00

您可以直接从匿名函数(通过闭包)访问 i,但您需要捕获它,以便每次调用 geocode 都能获得自己的副本。与 JavaScript 中通常的做法一样,添加另一个函数就可以解决问题。我重命名了外部 i 变量以使其更清晰:

for(var iter = 0; iter < 5; iter++) {
    (function(i) {
        geocoder.geocode({'address': address}, function(results, status) {
            // Geocoder stuff here
            // you can freely access i here
        });
    })(iter);
}

You can access i directly from you anonymous function (via closure), but you need to capture it so that each call to geocode gets its own copy. As usual in javascript, adding another function will do the trick. I renamed the outer i variable to make it clearer:

for(var iter = 0; iter < 5; iter++) {
    (function(i) {
        geocoder.geocode({'address': address}, function(results, status) {
            // Geocoder stuff here
            // you can freely access i here
        });
    })(iter);
}
我不会写诗 2024-10-05 11:02:00
function geoOuter(i) {
    geocoder.geocode({'address': address}, function(results, status) {
         // Geocoder stuff here
         // This has access to i in the outer function, which will be bound to
         // a different value of i for each iteration of the loop
     });
}

for(var i = 0; i < 5; i++) {
    geoOuter(i);
}

应该做...

function geoOuter(i) {
    geocoder.geocode({'address': address}, function(results, status) {
         // Geocoder stuff here
         // This has access to i in the outer function, which will be bound to
         // a different value of i for each iteration of the loop
     });
}

for(var i = 0; i < 5; i++) {
    geoOuter(i);
}

Oughta do it...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文