Connecting Tech Pros Worldwide Forums | Help | Site Map

Global Variables

iam_clint's Avatar
Forum Leader
 
Join Date: Jul 2006
Location: Oklahoma
Posts: 1,076
#1: Aug 28 '08
So.... I made some sample code to show you the issue..... I just stumbled upon this problem the other day.. its not really a question just putting it on here so people can comment about it. Test the code and as you can see i'm not globally declaring a single variable. however when you put variables without slapping the word var infront of them they automatically become global... this includes for (i=0)... I don't believe this is how its supposed to function.
Expand|Select|Wrap|Line Numbers
  1. <script>
  2. window.onload=function() {test();}
  3. /*######################## iam_clint ########################
  4.             Please take note that I am not globally declaring anything
  5.             if you change the for loops to be for(var i=0;   that actually resolves the problem.
  6.     ######################################################*/
  7. function test() {
  8. test_global="This isn't supposed to be global.";
  9.     for (i=0; i<=10; i++) {
  10.         alert("test " + i);
  11.         test2();
  12.     }
  13.     if (i>10) { alert("i is greater than 10 (" + i + ")"); }
  14.     test3();
  15. }
  16.  
  17. function test2() {
  18.     for (i=0; i<=10; i++) {
  19.             c=2;
  20.     }
  21. }
  22.  
  23. function test3() {
  24.     alert(test_global);
  25. }
  26. </script>
  27.  

Needs Regular Fix
 
Join Date: Jun 2006
Posts: 424
#2: Aug 28 '08

re: Global Variables


for (i=0;i<L;i++){}

The for syntax allows you to begin its loop with an existing counter (i) or to declare i with for(var i=// (some value).

If i is not declared (with var) in the function it is declared globally.
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,648
#3: Aug 28 '08

re: Global Variables


Quote:

Originally Posted by iam_clint

... this includes for (i=0)... I don't believe this is how its supposed to function.

do you mean that it outputs i = 12? but that would be the expected value. after leaving test2() with a value of 11 it gets the increment of the test() for loop adding up to 12 which it puts out (I needed a couple of Firebug tracings, though)
gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,134
#4: Aug 28 '08

re: Global Variables


yes that is right ... without declaring a variable the interpreter 'crawls' its way up from scope to scope to find any matching declared one ... assuming that it is already declared ... and when nothing matching is found on that way finally a global will be declared for it.

kind regards
iam_clint's Avatar
Forum Leader
 
Join Date: Jul 2006
Location: Oklahoma
Posts: 1,076
#5: Aug 28 '08

re: Global Variables


Its the only scripting that just randomly throws a variable to a global if its not defined as a var? its kinda like saying functions aren't independent
FLEB's Avatar
Newbie
 
Join Date: Aug 2008
Posts: 30
#6: Aug 28 '08

re: Global Variables


Quote:

Originally Posted by iam_clint

Its the only scripting that just randomly throws a variable to a global if its not defined as a var? its kinda like saying functions aren't independent

It's not random-- it's completely predictable. If the variable referenced does not exist, then one is created-- in the global scope. I'm not completely sure, but I would suppose this is merely a "fallback" functionality to allow looser programming and not throw a bunch of "Use of undeclared variable" errors.

You can use a var statement, outside any local scope, to declare a global. Purposely initializing global variables within another scope leads to poor readability, so you should probably just, as a rule, explicitly declare all variables with a properly-scoped var statement.
iam_clint's Avatar
Forum Leader
 
Join Date: Jul 2006
Location: Oklahoma
Posts: 1,076
#7: Aug 28 '08

re: Global Variables


thats obvious i'm not asking a question as read in the post. This really seems like a bug rather than a feature but thats just my observation.. with an example of how to replicate it.
gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,134
#8: Aug 28 '08

re: Global Variables


i'd rather agree with the understanding of this behaviour as a feature. when you put mozilla/FF into JavaScript strict mode you will always get a warning in the error-console that you assign a value to an undeclared variable. when you use a variable that way what should the interpreter do instead? within a local scope it will not find the declaration so it has to look up the next scope and at the end it could (and i would say it should!) give you an error that a declaration is needed, but it just creates a global then. it doesn't seem like a bug, it is more likely a fallback as it was mentioned in a previous post ...

kind regards
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#9: Aug 29 '08

re: Global Variables


Not a bug at all and completely expected. See, for example, this link.
iam_clint's Avatar
Forum Leader
 
Join Date: Jul 2006
Location: Oklahoma
Posts: 1,076
#10: Aug 29 '08

re: Global Variables


no sir i don't like it! :(
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#11: Aug 29 '08

re: Global Variables


Simple solution: use var when declaring variables.
Reply