Last active
August 29, 2015 14:05
-
-
Save mhairston/8fa008c22584a73cf1be to your computer and use it in GitHub Desktop.
closures via fairy tales
This file contains hidden or 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
// Great explanation of closures (plus I love this style of documentation): | |
// from http://stackoverflow.com/a/6472397 answer by Jacob Swartwood | |
// ------------------------------------------------------------------- // | |
// I'm a big fan of analogy and metaphor when explaining difficult concepts, so let me try my hand with a story. | |
// Once upon a time: | |
// There was a princess... | |
function princess() { | |
// She lived in a wonderful world full of adventures. She met her Prince Charming, rode around her world on a unicorn, battled dragons, encountered talking animals, and many other fantastical things. | |
var adventures = []; | |
function princeCharming() { /* ... */ } | |
var unicorn = { /* ... */ }, | |
dragons = [ /* ... */ ], | |
squirrel = "Hello!"; | |
// But she would always have to return back to her dull world of chores and grown-ups. | |
return { | |
// And she would often tell them of her latest amazing adventure as a princess. | |
story: function() { | |
return adventures[adventures.length - 1]; | |
} | |
}; | |
} | |
// But all they would see is a little girl... | |
var littleGirl = princess(); | |
// ...telling stories about magic and fantasy. | |
littleGirl.story(); | |
// And even though the grown-ups knew of real princesses, they would never believe in the unicorns or dragons because they could never see them. The grown-ups said that they only existed inside the little girl\'s imagination. | |
// But we know the real truth; that the little girl with the princess inside... | |
// ...is really a princess with a little girl inside. | |
// edited Nov 22 '12 at 15:47 | |
// community wiki | |
// Jacob Swartwood |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment