Strange problem with object property - present outside of loop, missing inside of loop
I have a really strange problem right now.
markers
is an array of objects.
0
content (String)
coords (Object)
gMarker (Object)
1
content (String)开发者_JAVA百科
coords (Object)
gMarker (Object)
2
content (String)
coords (Object)
gMarker (Object)
So far, so good.
The problem:
console.dir(markers);
/**
* output as expected
*/
for(var i in markers) {
console.dir(markers[i]);
/**
* gMarker object is missing!
* markers[i] suddenly consists just of content and coords
*/
How can that be?
Edit: It must have had something to do with the "load" callback function of google maps. It fires a bit too early, at least in my case. I replaced that callback (GEvent.addListener(map, "load", callback_fn)) with a simple setTimeout that fires my callback function after 2 seconds, and now everything works as expected.
Either way, this does not explain why the gMarker property is not available inside that for-loop, that can not even be a race condition since the loop starts immediately after that point where the object is still OK.
Shouldn't that be
for(var i in markers) {
console.dir(i);
It's weird, my Firebug console.dir doesn't even work with for-in on an array.
It could have to do with i
being a global variable set elsewhere, but more than likely this is called due to the property being loaded at a later point in time. Do you manually set the object, or how is it being loaded?
for (var i=markers.length;i--;){
console.dir(markers[i]);
}
精彩评论