What happens to a variable without the "var" keyword inside a function?
function bla() { a=5; }
Does a
automatically become a global variable?
And when exactly is it set? when the functions are being read for the first time and put into memory or o开发者_开发技巧nly at the execution of the function?
If you assign to a variable inside a function without declaring it with var
then it becomes a global.
The variable becomes a global when the function is first executed.
As soon as the function is executed, it puts the variable in global. Just like a function containing var a = 5
- it isn't executed until you actually call the function.
You can confirm this using a function: you don't get the alert until you call the function.
function x() {
alert(123);
return 1;
}
function bla() {
a = x();
}
精彩评论