Created
September 27, 2010 17:51
-
-
Save thegreatape/599474 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var foo = 'bar'; | |
var a = function(){ | |
console.log(foo); | |
} | |
var b = function(){ | |
console.log(foo); | |
var foo = 'blarg'; | |
} | |
a() | |
// 'bar' is logged. no surprises. | |
b() | |
// undefined is logged! defining a var *later* in the function | |
// prevents foo from being looked up in the closure scope |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See this explanation.
http://www.adequatelygood.com/2010/2/JavaScript-Scoping-and-Hoisting