且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Javascript全局变量作用域问题

更新时间:2023-02-26 08:04:59

someF creates a new (locally scoped) variable called someGlobal (which masks the global someGlobal) and assigns a value to it. It doesn't touch the global someGlobal (although cannot access it because there is another variable with the same name in scope).

var statements are hoisted, so someGlobal is masked for all of someF (not just after the var statement). The value of the local someGlobal is undefined until a value is assigned to it.

someF2 access the original (untouched) global someGlobal.