Skip to content

Instantly share code, notes, and snippets.

@nulayuhz
Created December 9, 2014 20:37
Show Gist options
  • Save nulayuhz/e0856b015ae5bb4aa332 to your computer and use it in GitHub Desktop.
Save nulayuhz/e0856b015ae5bb4aa332 to your computer and use it in GitHub Desktop.
hoisting
https://github.com/getify/You-Dont-Know-JS/blob/master/scope%20&%20closures/ch4.md
"When you see var a = 2;, you probably think of that as one statement.
But JavaScript actually thinks of it as two statements: var a; and a = 2;.
The first statement, the declaration, is processed during the compilation phase.
The second statement, the assignment, is left in place for the execution phase."
- declaration comes before the assignment
- function declarations are hoisted, but function expression are not
ex.
foo(); // TypeError
bar(); // ReferenceError
var foo = function bar() {
...
};
1. foo // TypeError: undefined is not a function
because var 'foo' is declared, and by default it is assigned to 'undefined'. It has not been assigned to be a function yet
2. bar //ReferenceError: bar is not defined
because the statement var foo = function bar() declares a variable 'foo',
but the assignment of function bar() {...} has not yet been executed, therefore, function bar() is
undefined
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment