473,386 Members | 1,764 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Global Variables

iam_clint
1,208 Expert 1GB
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.  
Aug 28 '08 #1
10 1192
mrhoo
428 256MB
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.
Aug 28 '08 #2
Dormilich
8,658 Expert Mod 8TB
... 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)
Aug 28 '08 #3
gits
5,390 Expert Mod 4TB
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
Aug 28 '08 #4
iam_clint
1,208 Expert 1GB
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
Aug 28 '08 #5
FLEB
30
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.
Aug 28 '08 #6
iam_clint
1,208 Expert 1GB
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.
Aug 28 '08 #7
gits
5,390 Expert Mod 4TB
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
Aug 28 '08 #8
acoder
16,027 Expert Mod 8TB
Not a bug at all and completely expected. See, for example, this link.
Aug 29 '08 #9
iam_clint
1,208 Expert 1GB
no sir i don't like it! :(
Aug 29 '08 #10
acoder
16,027 Expert Mod 8TB
Simple solution: use var when declaring variables.
Aug 29 '08 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Matt | last post by:
Greetings, What are people's thoughts on global variables in C++? Why are we taught not to use them in programming? Is it true that if you are running two copies of the C program one copy can...
4
by: Andrew V. Romero | last post by:
I have been working on a function which makes it easier for me to pull variables from the URL. So far I have: <script language="JavaScript"> var variablesInUrl; var vArray = new Array(); ...
12
by: David WOO | last post by:
Hi, I am a newbie on C++, I need to define some global variables which should be accessible to most classes. In the mean time, I don't won't the global variables be modified freely at most of...
2
by: Bryan Parkoff | last post by:
….I would like to know which is the best optimization to use global variable or global struct. I always tell C/C++ Compiler to turn on optimization. ….I use underscore between first name and...
17
by: MLH | last post by:
A97 Topic: If there is a way to preserve the values assigned to global variables when an untrapped runtime error occurs? I don't think there is, but I thought I'd ask. During development, I'm...
33
by: MLH | last post by:
I've read some posts indicating that having tons of GV's in an Access app is a bad idea. Personally, I love GVs and I use them (possibly abuse them) all the time for everything imaginable - have...
9
by: CDMAPoster | last post by:
About a year ago there was a thread about the use of global variables in A97: http://groups.google.com/group/comp.databases.ms-access/browse_frm/thread/fedc837a5aeb6157 Best Practices by Kang...
5
by: Sandman | last post by:
I dont think I understand them. I've read the section on scope in the manual inside out. I'm running PHP 5.2.0 Here is the code I'm working on: //include_me.php <?php $MYVAR = array(); global...
1
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have...
112
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.