Possible Duplicate:
JavaScript Variable Scope
I am (obviously) pretty new to Javascript, and I’ve seen several different points on other threads about the usage of Global and Local variables, but I’m trying to lock down some basic points on the subject in one place.
What is the difference between the following, declared outside (or used inside) of a function?
var thing = 1;
thing = 1;
I understand that using var declares the variable in it’s current scope. So leaving out var makes it global. What are some pitfalls that can show up? Could someone give a simple example of where variables might step on each other in this case?
Thanks in advance.
If you are in the global scope already, the difference is so small that you don’t have to worry about it
imagine that you had two
forloops, and you wanted to do something with them..
This piece of pseudo-code would work fine in many different languages.
The program would see an inner-loop and an outer-loop, and could tell that
iwasn’t referring to the same thing.In JavaScript, they’re the same
i.That’s because other languages have block-scope — any time you enter new curly braces, the program treats variables like they’re new.
In JavaScript there is only function scope, meaning that inside of functions variables are treated as new, but inside of control statements (
if,for,switch), they’re the same variable with the same value.So the outer loop sets
ito 0, and then goes into the inner loop.The inner loop goes through all of its list of items, and builds
iup as it goes…Then it goes back to the outer loop, and
istill equalsitems.length - 1…If that’s less than
elements.lengththen it adds one toi, which is now higher thanitemslength, so nothing happens in the inner loop, anymore……if
items.lengthis greater thanelements.lengthinstead, then the outer loop just ends after one time through.Now, I’m sure that you can start to think about times where you might want to use
xornameorvalueorelorsumoriordefaultorsrcorurlorimgorscript, etc several times in your whole program (tens of thousands of lines, even), and you can start to think about situations like that loop up above, where things could go wrong if you tried calling two different things by the same name.This is the same problem as var-fallthrough
If you have one function which uses a variable called
xand another function which uses another variable calledx, that’s great……unless you forget to declare the variable.
func1setwindow.x = 0;func2setwindow.x = "Bob";…if
window.xwas supposed to equal 42, for some other program to work right, now you have the potential to have 3 broken apps, just because of a few missingvars.It doesn’t instantly set the global variable, though. What it actually does is goes through the function chain. If you create a function inside of another function, then an undeclared var will look to its parent’s vars, and then its grandparent’s vars and then its great-grandparent’s vars…
If it gets all the way to
windowand nobody has a var of that name, then it creates one with that name onwindow.When you call func1, it sets its own
xto 0, and calls func2.func2 sets its own
yto 1.Then it sets func1’s
xto 2.Then, because func2 doesn’t have
zand func1 doesn’t havezandwindowdoesn’t havez, it setswindow.zto 3.That’s only the start of the confusion, and why its a very, very good idea to make sure that you’re defining vars which need to be available inside of that function (and in any functions created inside of that function)…
…and when you reference pre-existing vars, you reference them carefully, and know where that var is supposed to be, in your code (which function defined it… …so where on the chain the program is going to stop looking, before it gets to
window), and why you’re changing it from inside of another function.