Skip to content

Instantly share code, notes, and snippets.

@AliceWonderland
Forked from anonymous/7.2 Sniper Scope.js
Created April 18, 2017 07:26
Show Gist options
  • Save AliceWonderland/d8bf872d1b1e711c2c714c7aaead9bee to your computer and use it in GitHub Desktop.
Save AliceWonderland/d8bf872d1b1e711c2c714c7aaead9bee to your computer and use it in GitHub Desktop.
7.2 Sniper Scope created by smillaraaq - https://repl.it/HHjY/13
var badGuys = function() {
var bond = '007';
//does not get redefined and does know what bond is and its value
(function oddJob() {
var AgentinScope = (bond === '007'); //true
var prediction = 'Bond stays the same because it already exists and does not get redefined';
console.log("Bond in OddJob's scope", AgentinScope, "because " + prediction);
})();
//does not get redefined and does know what bond is and its value
//it exists but has not been assigned a value yet because it is an IIFE
(function goldFinger(bond) { //<== this param makes it local to this function now
console.log(bond); //bond=undefined
var AgentinScope = (bond == '007') //false
var prediction = 'Bond stays the same because it already exists and does not get redefined';
console.log("Bond in Gold Finger's scope", AgentinScope, "because " + prediction);
})(); //<== has no arg passed so bond is not defined. must be explicity passed.
//does not get redefined and does know what bond is and its value
(function scaramanga(target) {
var target = bond; //using the keyword var here will re-declare the target var. eventhough the target param in the parens already implicitly declares it with a var
var AgentinScope = (bond == '007')
var prediction = 'Bond stays the same because it already exists and does not get redefined';
console.log("Bond in Scaramanga's scope", AgentinScope, "because " + prediction);
})();
//redefines it and assigns a value and is not equal to 007
(function drNo() {
var bond = 'Body Double';
var AgentinScope = (bond === '007');
var prediction = 'Creates a new local variable that takes precedence over the global';
console.log("Bond in Dr. No's scope", AgentinScope, "because " + prediction);
})();
//agent will equal bond which equals body double, does not change the global bond
//agent will equal 007, local bond will equal body double, global bond does not change
(function Jaws(agent) {
console.log("Jaws", bond);
var agent = bond;
bond = 'Body Double'; //without the keyword var it made a change to the global var. it also changes the value for all the subsequent references
console.log("Jaws", agent);
var AgentinScope = (agent === '007');
var prediction = 'two predictions';
console.log("Bond in Jaws' scope", AgentinScope, "because " + prediction);
})();
//agent will equal bond which equals body double, does not change the global bond
//agent will equal 007, local bond will equal body double, global bond does not change
(function elChiffre() {
console.log("Chiff", bond);
var agent = bond; console.log("Chiff", agent);
bond = 'Body Double';
console.log("Chiff", agent);
var AgentinScope = (agent === '007');
var prediction = 'two predictions';
console.log("Bond in El Chiffre's scope", AgentinScope, "because " + prediction);
})();
}
// Run the function
badGuys()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment