Global Variables

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • iam_clint
    Recognized Expert Top Contributor
    • Jul 2006
    • 1207

    Global Variables

    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.
    Code:
    <script>
    window.onload=function() {test();}
    /*######################## iam_clint ########################
    			Please take note that I am not globally declaring anything
    			if you change the for loops to be for(var i=0;   that actually resolves the problem.
        ######################################################*/
    function test() {
    test_global="This isn't supposed to be global.";
    	for (i=0; i<=10; i++) {
    		alert("test " + i);
    		test2();
    	}
    	if (i>10) { alert("i is greater than 10 (" + i + ")"); }
    	test3();
    }
    
    function test2() {
    	for (i=0; i<=10; i++) {
    			c=2;
    	}
    }
    
    function test3() {
    	alert(test_global);
    }
    </script>
  • mrhoo
    Contributor
    • Jun 2006
    • 428

    #2
    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.

    Comment

    • Dormilich
      Recognized Expert Expert
      • Aug 2008
      • 8694

      #3
      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)

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5390

        #4
        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

        Comment

        • iam_clint
          Recognized Expert Top Contributor
          • Jul 2006
          • 1207

          #5
          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

          Comment

          • FLEB
            New Member
            • Aug 2008
            • 30

            #6
            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.

            Comment

            • iam_clint
              Recognized Expert Top Contributor
              • Jul 2006
              • 1207

              #7
              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.

              Comment

              • gits
                Recognized Expert Moderator Expert
                • May 2007
                • 5390

                #8
                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

                Comment

                • acoder
                  Recognized Expert MVP
                  • Nov 2006
                  • 16032

                  #9
                  Not a bug at all and completely expected. See, for example, this link.

                  Comment

                  • iam_clint
                    Recognized Expert Top Contributor
                    • Jul 2006
                    • 1207

                    #10
                    no sir i don't like it! :(

                    Comment

                    • acoder
                      Recognized Expert MVP
                      • Nov 2006
                      • 16032

                      #11
                      Simple solution: use var when declaring variables.

                      Comment

                      Working...