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 on marker = results[i]
). As marker
is passed into addEventListener
, 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.