[英]Function scopes and global variables
var foo = '1',
bar = '2';
console.log(foo, bar, window.foo); //1, 2, undefined
(function(foo){
console.log(foo, bar); //2, 2
})(bar);
I have two trivial questions regarding the code above: 关于上面的代码我有两个小问题:
Why is window.foo
undefined? 为什么
window.foo
未定义? Aren't all global variables attached to the window object by default? 默认情况下,并非所有全局变量都附加到窗口对象上吗?
Why is foo ===
2 inside of the closure? 为什么
foo ===
2在闭包内? I know that I'm passing the original bar
with the alias foo
, which is 2
, but outside of the function scope foo
is still 1
. 我知道我正在传递带有别名
foo
的原始bar
,它是2
,但在函数范围之外foo
仍然是1
。 And as far as I know, the original foo
can be accessed from inside of the closure as well. 据我所知,原始的
foo
也可以从闭包内部访问。 Is the "new foo" prioritized since it's passed as an argument to the IIFE? “新foo”是否被优先考虑,因为它作为IIFE的论据被传递?
Why is
window.foo
undefined?为什么
window.foo
未定义? Ins't all "global" variables automatically attached to the window object?不是所有“全局”变量都自动附加到窗口对象上吗?
Yes, global variables become properties of window
, but the code is not run in global scope in your fiddle. 是的,全局变量成为
window
属性,但代码不是在小提琴中的全局范围内运行。 It is run in the load
event handler (see the second checkbox on the left hand side, it says "onLoad"). 它在
load
事件处理程序中运行(请参阅左侧的第二个复选框,它显示“onLoad”)。 Here it is run in global scope: http://jsfiddle.net/GbeDX/1/ 它在全球范围内运行: http : //jsfiddle.net/GbeDX/1/
Why is
foo === 2
inside of the closure?为什么
foo === 2
在闭包内? [...] And as far as I know, the originalfoo
can be accessed from inside of the closure as well.[...]据我所知,原始的
foo
也可以从闭包内部访问。
No, it can't. 不,它不能。 The parameter
foo
shadows the variable foo
. 参数
foo
隐藏变量foo
。 If it is a global variable though, you can access it with window.foo
. 如果它是一个全局变量,你可以使用
window.foo
访问它。
Yes. 是。 My guess is you execute that code in some debug environment like Firebug, jsFiddle, etc., which implicitly wraps that code into a scope
我的猜测是你在一些调试环境中执行代码,如Firebug,jsFiddle等,它隐式地将代码包装到一个范围内
You pass the value 2
into that self-executing anonymous function, and you access the value via the local argument foo
. 您将值
2
传递给该自执行匿名函数,并通过本地参数foo
访问该值。 Since the scope chain lookup always works bottom->up , the resolving for that variable name stops at the local scope, where foo
is found as argument. 由于范围链查找总是在底部向上工作 ,因此该变量名称的解析在本地范围停止,其中
foo
被作为参数找到。
window.foo === '1'
, unless you've run this inside of another function. window.foo === '1'
,除非你在另一个函数中运行它。 foo === '2'
inside of the closure becuase you declared foo
as an input argument to the function. foo === '2'
在闭包内部因为你声明foo
作为函数的输入参数。 You could access the globally scoped foo
if you hadn't decided to declare a locally scoped variable with the same name. foo
。 foo
as undefined
because you are running your script in a debug environment like firebug or jsfiddle (related https://stackoverflow.com/a/11572991/975520 ) foo
是undefined
因为你在调试环境中运行你的脚本,如firebug或jsfiddle(相关的https://stackoverflow.com/a/11572991/975520 ) foo
overwrite the variable foo
. foo
覆盖变量foo
。 But it's still a global variable, so you can use it as window.foo
. window.foo
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.