Skip to content

Instantly share code, notes, and snippets.

@vosechu
Created February 17, 2013 19:12
Show Gist options
  • Save vosechu/4972906 to your computer and use it in GitHub Desktop.
Save vosechu/4972906 to your computer and use it in GitHub Desktop.
Exploration of scoping, hoisting, and shadowing in javascript
// At all times you can use the debugger to break into the console in Chrome
debugger;
// Demonstration of Scoping
var global = "I'm alive!";
function() { // IIFE style function definition (note the parens at the end of the curly braces)
// Point 1: Does global exist here?
// If I define a variable here does it exist outside the IIFE?
var inner_variable = "I'm alive inside!"
var inner = function () { // Lambda style function definition
// Point 2: Does global exist here?
// If I define a variable here does it exist at Point 1?
// If I define a variable here does it exist at Point 3? How about at Point 7?
// What happens if I override the variable from Point 1 here (without)
// the var keyword? How does that change the value
function inner_inner() { // Normal function definition
// Point 3:
};
inner_inner();
// Point 4:
if(true) {
// Point 5:
}
// Point 6:
};
inner();
// Point 7:
}();
// Point 8: What is alive at this point?
for(var b in window) {
if(window.hasOwnProperty(b)) console.log(b);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment