Help | Site Map
Connecting Tech Pros Worldwide
Reply
 
LinkBack Thread Tools
  #1  
Old August 28th, 2008, 02:53 PM
iam_clint's Avatar
Forum Leader
 
Join Date: Jul 2006
Location: Oklahoma
Age: 21
Posts: 1,032
Default 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.
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.  
Reply
  #2  
Old August 28th, 2008, 03:20 PM
Needs Regular Fix
 
Join Date: Jun 2006
Posts: 411
Default

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.
Reply
  #3  
Old August 28th, 2008, 03:42 PM
Dormilich's Avatar
Expert
 
Join Date: Aug 2008
Location: Leipzig, Germany
Age: 31
Posts: 646
Default

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)
Reply
  #4  
Old August 28th, 2008, 03:52 PM
gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Age: 36
Posts: 3,389
Default

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

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

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

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.
Reply
  #8  
Old August 28th, 2008, 06:16 PM
gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Age: 36
Posts: 3,389
Default

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

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

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

Simple solution: use var when declaring variables.
Reply
Reply

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

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 network members.
Post your question now . . .
It's fast and it's free

Popular Articles