Skip to content

Instantly share code, notes, and snippets.

@glinesbdev
Last active August 29, 2015 14:07
Show Gist options
  • Save glinesbdev/6c08b8fb403c8ac26c9a to your computer and use it in GitHub Desktop.
Save glinesbdev/6c08b8fb403c8ac26c9a to your computer and use it in GitHub Desktop.
Fucking JS Closure
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