Skip to content

Instantly share code, notes, and snippets.

@mkuklis
Created June 7, 2011 01:25
Show Gist options
  • Save mkuklis/1011501 to your computer and use it in GitHub Desktop.
Save mkuklis/1011501 to your computer and use it in GitHub Desktop.
JavaScript Hoisting
//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