Created
March 12, 2016 17:13
-
-
Save anonymous/afaaa2118d0e0e87185d to your computer and use it in GitHub Desktop.
Scope Basics Some SCOPE basics // source https://jsbin.com/yolitiz
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta name="description" content="Some SCOPE basics"> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>Scope Basics</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
// SCOPE (not talking about ES2015 here) | |
var globalVariable = "bad"; | |
// I'm an IIFE (Immediate-invoked function expression) | |
// *************************************************** | |
(function(){ | |
stillAGlobal= "still bad"; | |
// Use "var" to make it function scoped | |
var functionScope = "good"; | |
for(i=0;i<=10;i++){ | |
// Nothing here | |
} | |
for(var j=0;j<=10;j++){ | |
// Nothing here | |
} | |
console.log("***************** First IIFE *****************************"); | |
console.log("globalVariable:" + globalVariable); | |
console.log("stillAGlobal:" + stillAGlobal); | |
console.log("functionScope:" + functionScope); | |
console.log("i : " + i); | |
console.log("j : " + i); | |
console.log("**********************************************************"); | |
})(); | |
// *************************************************** | |
// I'm a new IIFE | |
// *************************************************** | |
(function(){ | |
var functionScope; | |
console.log("***************** Second IIFE *****************************"); | |
console.log("globalVariable:" + globalVariable); | |
console.log("stillAGlobal:" + stillAGlobal); | |
console.log("functionScope:" + functionScope ); | |
console.log("i : " + i); | |
console.log("j : " + j); | |
console.log("***********************************************************"); | |
})(); | |
// *************************************************** | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">// SCOPE (not talking about ES2015 here) | |
var globalVariable = "bad"; | |
// I'm an IIFE (Immediate-invoked function expression) | |
// *************************************************** | |
(function(){ | |
stillAGlobal= "still bad"; | |
// Use "var" to make it function scoped | |
var functionScope = "good"; | |
for(i=0;i<=10;i++){ | |
// Nothing here | |
} | |
for(var j=0;j<=10;j++){ | |
// Nothing here | |
} | |
console.log("***************** First IIFE *****************************"); | |
console.log("globalVariable:" + globalVariable); | |
console.log("stillAGlobal:" + stillAGlobal); | |
console.log("functionScope:" + functionScope); | |
console.log("i : " + i); | |
console.log("j : " + i); | |
console.log("**********************************************************"); | |
})(); | |
// *************************************************** | |
// I'm a new IIFE | |
// *************************************************** | |
(function(){ | |
var functionScope; | |
console.log("***************** Second IIFE *****************************"); | |
console.log("globalVariable:" + globalVariable); | |
console.log("stillAGlobal:" + stillAGlobal); | |
console.log("functionScope:" + functionScope ); | |
console.log("i : " + i); | |
console.log("j : " + j); | |
console.log("***********************************************************"); | |
})(); | |
// ***************************************************</script></body> | |
</html> |
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
// SCOPE (not talking about ES2015 here) | |
var globalVariable = "bad"; | |
// I'm an IIFE (Immediate-invoked function expression) | |
// *************************************************** | |
(function(){ | |
stillAGlobal= "still bad"; | |
// Use "var" to make it function scoped | |
var functionScope = "good"; | |
for(i=0;i<=10;i++){ | |
// Nothing here | |
} | |
for(var j=0;j<=10;j++){ | |
// Nothing here | |
} | |
console.log("***************** First IIFE *****************************"); | |
console.log("globalVariable:" + globalVariable); | |
console.log("stillAGlobal:" + stillAGlobal); | |
console.log("functionScope:" + functionScope); | |
console.log("i : " + i); | |
console.log("j : " + i); | |
console.log("**********************************************************"); | |
})(); | |
// *************************************************** | |
// I'm a new IIFE | |
// *************************************************** | |
(function(){ | |
var functionScope; | |
console.log("***************** Second IIFE *****************************"); | |
console.log("globalVariable:" + globalVariable); | |
console.log("stillAGlobal:" + stillAGlobal); | |
console.log("functionScope:" + functionScope ); | |
console.log("i : " + i); | |
console.log("j : " + j); | |
console.log("***********************************************************"); | |
})(); | |
// *************************************************** |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment