Created
June 7, 2011 01:25
-
-
Save mkuklis/1011501 to your computer and use it in GitHub Desktop.
JavaScript Hoisting
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
//1. variable declaration: | |
// declaration | |
function foo() { | |
return true; | |
var x = 1; | |
} | |
// interpretation | |
funciton foo() { | |
var x; | |
return true; | |
x = 1; | |
} | |
//2. function declaration: | |
// declaration | |
function foo() { | |
foo1(); // all good | |
foo2(); // TypeError "foo2 is not a function" | |
foo3(); // TypeError "foo3 is not a function" | |
function foo1() {}; // function declaration | |
var foo2 = function () {}; // function expression | |
var foo3 = function foo4() {}; // named function expression | |
} | |
// interpretation | |
function foo() { | |
function foo1() {} | |
var foo2; | |
var foo3; | |
foo1(); | |
foo2(); | |
foo2 = function () {}; | |
foo3 = function foo4() {}; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment