简体   繁体   English

在JavaScript中,当通过FOR循环循环时,如何将数组中该项的值传递给匿名函数?

[英]In JavaScript, when looping through a FOR loop, how do I pass the value of the item in an array to an anonymous function?

In JavaScript, I need to loop through a for loop to get the value of an item in an array, and pass this to an anonymous function. 在JavaScript中,我需要遍历for循环以获取数组中一项的值,并将其传递给匿名函数。 A simplified example of this is below: 下面是一个简化的示例:

var aFunctions = []; 
var aStrings = ["a","b","c"];
for (var i = 0; i < aStrings.length - 1; i++) {
    aFunctions[i] = function () { alert(aStrings[i]); };
}
aFunctions[0](); //alerts "c" instead of "a"

I can see why this is happening - the variable i is being set to 2 when the loop exits, and then when I call aFunctions[0]() , the function fires off and evaluates aStrings[i] rather than aStrings[0] . 我可以看到发生这种情况的原因-循环退出时变量i被设置为2 ,然后当我调用aFunctions[0]() ,该函数触发并评估aStrings[i]而不是aStrings[0]

What I want to know is how to get the value of aStrings[i] returned within my for loop, and not when it's executed. 我想知道的是如何获取在我的for循环中返回的aStrings[i]的值,而不是在执行时。


To give more specific detail, I'm working on a Google Maps page, and the markers are stored in a JavaScript array client-side, and in a DB server-side. 为了提供更多具体细节,我正在Google Maps页面上工作,这些标记存储在JavaScript数组客户端和数据库服务器端。 I write the array at page-load and after that's complete I want to generate the markers, and give them each a custom InfoWindow with the text set to an HTML string. 我在页面加载时编写数组,完成后我要生成标记,并为每个标记提供一个自定义InfoWindow,并将文本设置为HTML字符串。 This is the specific code I use: 这是我使用的特定代码:

for (var i = 0; i < tGoogleMarker.length - 1; i++) {
    var text = tGoogleMarker[i][4];
    var marker = new google.maps.Marker({
                        position: new google.maps.LatLng(tGoogleMarker[i][0], tGoogleMarker[i][1]),
                        map: map,
                        flat: false,
                        icon: tGoogleMarker[i][2],
                        shadow: tGoogleMarker[i][3]
                        });
    google.maps.event.addListener(marker, 'click', function () { ShowMarkerContentPopUp(this, text); });
}

Instead of getting the HTML string of the specific marker, I get the text of the last item in the array used for all markers. 我没有获取特定标记的HTML字符串,而是获取了用于所有标记的数组中最后一项的文本。

You need to use closure. 您需要使用闭包。 Read More about this here: How do JavaScript closures work? 在此处阅读有关此内容的更多信息: JavaScript闭包如何工作?

 var aFunctions = []; var aStrings = ["a","b","c"]; for (var i = 0; i < aStrings.length - 1; i++) { aFunctions[i] = (function(val){ return function() { alert(val) } })(aStrings[i]); } aFunctions[0](); //alerts "c" instead of "a" 

You can use a closure around the addListener part: 您可以在addListener部分周围使用闭包:

(function(text){
    google.maps.event.addListener(marker, 'click', function () { ShowMarkerContentPopUp(this, text); });
})(text);

This creates a new scope and takes the text variable as an argument. 这将创建一个新的作用域,并将text变量作为参数。 It means the text inside the closure is protected from being changed outside of the closure. 这意味着保护闭包内部的text不会在闭包外部被更改。

The same can be done to solve your first example: 可以通过同样的方法解决您的第一个示例:

for (var i = 0; i < aStrings.length - 1; i++) {
    (function(i){
        aFunctions[i] = function () { alert(aStrings[i]); };
    })(i);
}

More info on closures: 有关闭包的更多信息:

var aFunctions = []; 
var aStrings = ["a","b","c"];
for (var i = 0; i < aStrings.length; i++) {
    aFunctions[i] =  (function(arg) { return function() { alert(arg); };})(aStrings[i]) ;
}
aFunctions[0](); // alerts "a"
aFunctions[2](); // alerts "c"

http://jsfiddle.net/dbtncehf/ http://jsfiddle.net/dbtncehf/

Well, you can avoid needing to worry about closures much and still achieve the necessary results. 好吧,您可以避免担心太多的closures ,而仍然可以达到必要的结果。 It can be done by attaching each text variable to its respective marker. 可以通过将每个text变量附加到其各自的标记来完成。 This way your anonymous function can call it using this.text 这样,您的匿名函数可以使用this.text进行调用

for (var i = 0; i < tGoogleMarker.length - 1; i++) {
var marker = new google.maps.Marker({
                    text: tGoogleMarker[i][4],
                    position: new google.maps.LatLng(tGoogleMarker[i][0], tGoogleMarker[i][1]),
                    map: map,
                    flat: false,
                    icon: tGoogleMarker[i][2],
                    shadow: tGoogleMarker[i][3]
                    });
google.maps.event.addListener(marker, 'click', function () { ShowMarkerContentPopUp(this, this.text); });

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过引用遍历数组来遍历javascript中的函数 - Looping through an array with pass by reference to a function in javascript 如何将参数传递给 JavaScript 函数中的匿名函数? - How do I pass arguments to an anonymous function within a function in JavaScript? 如何将参数传递给匿名Javascript函数? - How do I pass argument to anonymous Javascript function? 如何将字符串的匿名数组传递给JavaScript函数? - How to pass an anonymous array of strings to a JavaScript function? 如何通过值将变量传递给匿名javascript函数? - How to pass a variable by value to an anonymous javascript function? JavaScript 滤波器循环未循环遍历数组中的所有项目 - JavaScript Filter Loop Not Looping Through All Item In Array 在 Javascript 中,循环对象数组时,如何将每个元素的名称发送到控制台? - In Javascript, when looping through an array of objects, how do I send each element's name to the console? 循环不遍历数组的JavaScript - Javascript for loop not looping through array 通过javascript中的值传递匿名函数? - Pass anonymous function by value in javascript? 如何循环遍历按钮数组,并且只有在用户单击数组中的下一个按钮时才继续循环 - How do I loop through an array of buttons and only continue looping if the user clicks the next button in the array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM