 | 
August 28th, 2008, 02:53 PM
|  | Forum Leader | | Join Date: Jul 2006 Location: Oklahoma Age: 22
Posts: 1,038
| | 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. -
<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>
-
| 
August 28th, 2008, 03:20 PM
| | Needs Regular Fix | | Join Date: Jun 2006
Posts: 415
| | 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.
| 
August 28th, 2008, 03:42 PM
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany Age: 31
Posts: 895
| | 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)
| 
August 28th, 2008, 03:52 PM
|  | Moderator | | Join Date: May 2007 Location: Munich, Germany Age: 36
Posts: 3,448
| |
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
| 
August 28th, 2008, 04:42 PM
|  | Forum Leader | | Join Date: Jul 2006 Location: Oklahoma Age: 22
Posts: 1,038
| |
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
| 
August 28th, 2008, 05:16 PM
|  | Newbie | | Join Date: Aug 2008
Posts: 30
| | 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.
| 
August 28th, 2008, 05:34 PM
|  | Forum Leader | | Join Date: Jul 2006 Location: Oklahoma Age: 22
Posts: 1,038
| |
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.
| 
August 28th, 2008, 06:16 PM
|  | Moderator | | Join Date: May 2007 Location: Munich, Germany Age: 36
Posts: 3,448
| |
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
| 
August 29th, 2008, 06:14 PM
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 12,964
| |
Not a bug at all and completely expected. See, for example, this link.
| 
August 29th, 2008, 07:23 PM
|  | Forum Leader | | Join Date: Jul 2006 Location: Oklahoma Age: 22
Posts: 1,038
| |
no sir i don't like it! :(
| 
August 29th, 2008, 09:17 PM
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 12,964
| |
Simple solution: use var when declaring variables.
|  | | Thread Tools | Search this Thread | | | |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 205,248 network members.
|