Last active
December 11, 2015 05:59
-
-
Save n8kowald/4556515 to your computer and use it in GitHub Desktop.
Notes on the name of JavaScript syntax and also rules.
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
/* RULES */ | |
- Variable and function declarations are hoisted (names are moved to the top of their current scope) by the JavaScript interpreter. | |
- Variable initialisations are not moved. | |
- Only functions create new scope | |
- Function Declarations are officially prohibited within non-function blocks (such as if) | |
var a; // variable declaration | |
a = 1; // variable initialisation | |
- The function name (if any) of a function expression is not visible outside of it’s scope. | |
- The function name of a function declaration is visible within it’s scope and the scope of it’s parent | |
- Now in practice, generally you want to put your var declarations at the top of the function, since that is where they end up anyway. | |
/* NAMES */ | |
Function Declaration | |
-------------------- | |
function bar() { | |
return 3; | |
} | |
Function Expressions | |
-------------------- | |
//anonymous function expression | |
var a = function() { | |
return 3; | |
} | |
//named function expression | |
var a = function bar() { | |
return 3; | |
} | |
//self invoking function expression | |
(function sayHello() { | |
alert("hello!"); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment