Created
June 20, 2012 18:24
-
-
Save cowboy/2961408 to your computer and use it in GitHub Desktop.
javascript: function declaration vs expression
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
function foo(state) { | |
function bar() { return 1; } | |
return state ? bar : null; | |
} | |
foo(false) // null (bar is created but not used) | |
foo(true)() // 1 (bar is created and returned) | |
function foo(state) { | |
return state ? function() { return 1; } : null; | |
} | |
foo(false) // null (the anonymous function isn't even created) | |
foo(true)() // 1 (the anonymous function is created and returned) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks. \o/