Jump to content

Why does the var keyword make a difference?


ShadowMage

Recommended Posts

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

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

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

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" properties
but 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

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...