Last active
August 29, 2015 14:07
-
-
Save glinesbdev/6c08b8fb403c8ac26c9a to your computer and use it in GitHub Desktop.
Fucking JS Closure
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
function makeTargetAssigner(sharks, targets) { | |
return function(shark) { | |
for (var i = 0; i < sharks.length; i++) { | |
if (shark == sharks[i]) { | |
alert("What up, " + shark + "!" + "\n" + "There've been " + targets[i] + " sightings in our 'hood!" + "\n" + "Time for a swim-by lasering, homie!"); | |
} | |
} | |
}; | |
} | |
/* Pass in this array first */ | |
var listOfSharks = ["Sea Pain", "Great Wheezy", "DJ Chewie", "Lil' Bitey", "Finmaster Flex", "Swim Khalifa", "Ice Teeth", "The Notorious J.A.W."] | |
/* Pass in is array second */ | |
var listOfTargets = ["icicle bat", "snow yeti", | |
"killer penguin", "frost tiger", | |
"polar bear", "iceberg", | |
"blue witch", "wooly mammoth"]; | |
/* Run this code */ | |
var getTargetFor = makeTargetAssigner( listOfSharks, | |
listOfTargets ); | |
/* Then this code */ | |
getTartgetFor("Ice Teeth"); // can be replaced with any sharks name |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment