ShadowMage Posted February 23, 2011 Share Posted February 23, 2011 Quick question. I had a for loop which I initialized like this:for (x=0, y=_dOpenWindows.length; x<y; x++)I realized that the loop was terminating early so I investigated and found that y was equal to 1. Yet there were 3 elements in the _dOpenWindows array. I am not sure how y was being set to 1. On a wild guess, which I didn't really expect to hold true, I assumed it was pulling a global value from somewhere so I added the var keyword to declare the variables locally:for (var x=0, y=_dOpenWindows.length; x<y; x++)To my surprise, it worked!But, what I can't figure out is why it would set y to 1 when I was clearly setting it to the length of the array... :)Any insight would be appreciated. Link to comment Share on other sites More sharing options...
thescientist Posted February 23, 2011 Share Posted February 23, 2011 when did it tell you y == 1? alert(y); //here?for (x=0, y=_dOpenWindows.length; x<y; x++){ alert(y); //or here? ...}; Link to comment Share on other sites More sharing options...
ShadowMage Posted February 23, 2011 Author Share Posted February 23, 2011 In Firebug as I was looping through it (using breakpoints, I rarely use alerts ). So I guess at your second alert. Link to comment Share on other sites More sharing options...
thescientist Posted February 23, 2011 Share Posted February 23, 2011 i guess without seeing the whole loop, there's a random chance it could be over written by a global variable, but without that, that seems odd. I guess the var forces the scope of y to whatever curly braces the loop is in at the time, "preventing" it from being over written by some mysterious global. Link to comment Share on other sites More sharing options...
boen_robot Posted February 23, 2011 Share Posted February 23, 2011 You might have been reffering to the event's "x" and "y" properties (see bottom of this page). But yeah, forcing variables intended to be local be local is of course the best course of action. Link to comment Share on other sites More sharing options...
ShadowMage Posted February 24, 2011 Author Share Posted February 24, 2011 i guess without seeing the whole loop, there's a random chance it could be over written by a global variable, but without that, that seems odd. I guess the var forces the scope of y to whatever curly braces the loop is in at the time, "preventing" it from being over written by some mysterious global. There really isn't much to the loop:for (var x=0, y=_dOpenWindows.length; x<y; x++) { if (_dOpenWindows[x] !== me) { _dOpenWindows[x].blur(); }} I think boen might be on to something though: You might have been reffering to the event's "x" and "y" propertiesbut as to why it would equal 1 I'm not sure. This loop runs when I click on one the divs I'm using as a "window" to provide "focus" to that div (ie, it brings it out in front of any other "windows" that might be open) so I would think that y should be equal to the y value of the mouse coordinates, would it not? Link to comment Share on other sites More sharing options...
boen_robot Posted February 24, 2011 Share Posted February 24, 2011 Perhaps that's not the case for the... what event do you attach this to again? Whatever the case, in this event the information is probably not populated, and remains with its default values, which are 1. Link to comment Share on other sites More sharing options...
ShadowMage Posted February 24, 2011 Author Share Posted February 24, 2011 Perhaps that's not the case for the... what event do you attach this to again? Whatever the case, in this event the information is probably not populated, and remains with its default values, which are 1.It is fired once on mouse down and once on click. Those should use these properties... Link to comment Share on other sites More sharing options...
ShadowMage Posted February 24, 2011 Author Share Posted February 24, 2011 I just realized that the link you provided before states that the x/y properties are not supported in FireFox, yet it is in FireFox (using Firebug) where I observed the odd behavior.... :)This adds even more mystery... Link to comment Share on other sites More sharing options...
boen_robot Posted February 24, 2011 Share Posted February 24, 2011 My guess is that they added this value for the sake of compatibility, but won't claim support until they actually populate the values. Link to comment Share on other sites More sharing options...
ShadowMage Posted February 24, 2011 Author Share Posted February 24, 2011 My guess is that they added this value for the sake of compatibility, but won't claim support until they actually populate the values.Ah, I suppose that makes sense. Thanks, boen! Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.