Created
September 15, 2014 15:44
-
-
Save paulwsmith/e0ac93efbfe8e0474b7e to your computer and use it in GitHub Desktop.
JS global variable access
This file contains 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
myGlobal = 'I am accessible!'; // global var | |
(function(window) { | |
console.log('Without clearing anything'); | |
console.log(window.myGlobal); // 'I am accessible!' | |
console.log(myGlobal); // 'I am accessible!' | |
})(window); | |
(function(window) { | |
console.log('Clearing window variable by passing in empty object'); | |
console.log(window.myGlobal); // undefined | |
console.log(myGlobal); // 'I am accessible!' | |
})({}); | |
(function(window) { | |
console.log('"Clearing" myGlobal var by defining a private variable w/ same name'); | |
var myGlobal; | |
console.log(window.myGlobal); // 'I am accessible!' | |
console.log(myGlobal); // undefined | |
})(window); | |
(function(window) { | |
console.log('Clearing both ways of accessing myGlobal'); | |
var myGlobal; | |
console.log(window.myGlobal); // undefined | |
console.log(myGlobal); // undefined | |
})({}); | |
// All clearing was local only, outside of those functions it's untouched | |
console.log('Outside of function runs'); | |
console.log(window.myGlobal); // 'I am accessible!' | |
console.log(myGlobal); // 'I am accessible!' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment