开发者

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();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜