简体   繁体   English

函数范围和全局变量

[英]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: 关于上面的代码我有两个小问题:

  1. Why is window.foo undefined? 为什么window.foo未定义? Aren't all global variables attached to the window object by default? 默认情况下,并非所有全局变量都附加到窗口对象上吗?

  2. 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的论据被传递?

http://jsfiddle.net/GbeDX/ http://jsfiddle.net/GbeDX/

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 original foo 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访问它。

  1. 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等,它隐式地将代码包装到一个范围内

  2. 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被作为参数找到。

  1. window.foo === '1' , unless you've run this inside of another function. window.foo === '1' ,除非你在另一个函数中运行它。
  2. 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
  1. Yes. 是。 Probably you are seeing the variable foo as undefined because you are running your script in a debug environment like firebug or jsfiddle (related https://stackoverflow.com/a/11572991/975520 ) 可能你看到变量fooundefined因为你在调试环境中运行你的脚本,如firebug或jsfiddle(相关的https://stackoverflow.com/a/11572991/975520
  2. No. The parameter 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM