Skip to content

Instantly share code, notes, and snippets.

@DaveWelling
Forked from anonymous/index.html
Created March 12, 2016 17:15
Show Gist options
  • Save DaveWelling/9922f77461c7dd3f8f49 to your computer and use it in GitHub Desktop.
Save DaveWelling/9922f77461c7dd3f8f49 to your computer and use it in GitHub Desktop.
Scope Basics Some SCOPE basics // source https://jsbin.com/yolitiz
<!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>
// 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