Function scopes and global variables
javascript
Solution
Why is `window.foo` undefined? 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. It is run in the `load` event handler (see the second checkbox on the left hand side, it says "onLoad"). Here it is run in global scope: http://jsfiddle.net/GbeDX/1/
Why is `foo === 2` inside of the closure? [...] And as far as I know, the original `foo` can be accessed from inside of the closure as well.
No, it can't. The parameter `foo` shadows the variable `foo`. If it is a global variable though, you can access it with `window.foo`.
Problem
``` 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? Aren't all global variables attached to the window object by default? Why is `foo ===`2 inside of the closure? 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`. And as far as I know, the original `foo` can be accessed from inside of the closure as well. Is the "new foo" prioritized since it's passed as an argument to the IIFE? http://jsfiddle.net/GbeDX/