Created
November 7, 2011 11:43
-
-
Save gaurish/1344753 to your computer and use it in GitHub Desktop.
JavaScript Function Declaration Patterns
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
// Style #1 | |
function foo() { | |
//do something | |
} | |
//Style #2 | |
var foo = function(){ | |
//do something | |
} | |
//Style #3 | |
var foo = function bar(){ | |
//do something | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Style 1. is a statement/declaration that creates a function named foo with a reference called foo
Style 2. is an expression that creates an anonymous function & assigns it to a reference called foo
Style 3. ia an expression that creates a function named bar & assigns it to a reference called foo
Style-1 is hoisted, so you can use the function before the actual definition.
foo() before style 2 or 3 will give an error.
Also during function calls & error-stack generation the function name is used.
Extensive info at http://stackoverflow.com/questions/336859/javascript-var-functionname-function-vs-function-functionname