This question already has an answer here:
Here is a simplified version of something I'm trying to run:
for (var i = 0; i < results.length; i++) {
marker = results[i];
google.maps.event.addListener(marker, 'click', function() {
change_selection(i);
});
}
but I'm finding that every listener uses the value of results.length (the value when the for loop terminates). How can I add listeners such that each uses the value of i at the time I add it, rather than the reference to i?
You're winding up with a closure.
Here's an article on closures and how to work with them. Check out Example 5 on the page; that's the scenario you're dealing with.EDIT: Four years later, that link is dead. The root of the issue above is that the
for
loop forms closures (specifically onmarker = results[i]
). Asmarker
is passed intoaddEventListener
, you see the side effect of the closure: the shared "environment" is updated with each iteration of the loop, before it's finally "saved" via the closure after the final iteration. MDN explains this very well.I think we can define a temporary variable to store the value of i.
I haven't tested it though.
closures:
EDIT, 2013: These are now commonly referred to as an IIFE
In modern browsers, you can use the
let
orconst
keywords to create a block-scoped variable:In older browsers, you need to create a separate scope that saves the variable in its current state by passing it as a function parameter:
By creating an anonymous function and calling it with the variable as the first argument, you're passing-by-value to the function and creating a closure.
As well as the closures, you can use
function.bind
:passes the value of
i
in as an argument to the function when called. (null
is for bindingthis
, which you don't need in this case.)function.bind
was introduced by the Prototype framework and has been standardised in ECMAScript Fifth Edition. Until browsers all support it natively, you can add your ownfunction.bind
support using closures: