Connecting Tech Pros Worldwide Help | Site Map

Global Variables

  #1  
Old August 28th, 2008, 02:53 PM
iam_clint's Avatar
Forum Leader
 
Join Date: Jul 2006
Location: Oklahoma
Posts: 1,077
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.  
  #2  
Old August 28th, 2008, 03:20 PM
Needs Regular Fix
 
Join Date: Jun 2006
Posts: 425

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.
  #3  
Old August 28th, 2008, 03:42 PM
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,487
Provided Answers: 9

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)
  #4  
Old August 28th, 2008, 03:52 PM
gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,102
Provided Answers: 1

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
  #5  
Old August 28th, 2008, 04:42 PM
iam_clint's Avatar
Forum Leader
 
Join Date: Jul 2006
Location: Oklahoma
Posts: 1,077

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
  #6  
Old August 28th, 2008, 05:16 PM
FLEB's Avatar
Newbie
 
Join Date: Aug 2008
Posts: 30

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.
  #7  
Old August 28th, 2008, 05:34 PM
iam_clint's Avatar
Forum Leader
 
Join Date: Jul 2006
Location: Oklahoma
Posts: 1,077

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.
  #8  
Old August 28th, 2008, 06:16 PM
gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,102
Provided Answers: 1

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
  #9  
Old August 29th, 2008, 06:14 PM
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,521
Provided Answers: 12

re: Global Variables


Not a bug at all and completely expected. See, for example, this link.
  #10  
Old August 29th, 2008, 07:23 PM
iam_clint's Avatar
Forum Leader
 
Join Date: Jul 2006
Location: Oklahoma
Posts: 1,077

re: Global Variables


no sir i don't like it! :(
  #11  
Old August 29th, 2008, 09:17 PM
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,521
Provided Answers: 12

re: Global Variables


Simple solution: use var when declaring variables.
Reply


Similar Threads
Thread Thread Starter Forum Replies Last Post
The Case Against Global Variables weaknessforcats insights 1 December 20th, 2007 06:04 AM
global variables Sandman answers 5 February 15th, 2007 12:05 AM
Are static variables useful for replacing global variables? CDMAPoster@FortuneJames.com answers 9 November 15th, 2006 03:55 AM
Can I prevent the loss of the values assigned to global variables after an untrapped run-time error? MLH answers 17 November 13th, 2005 03:32 PM
Global Variables Matt answers 10 July 22nd, 2005 04:27 AM