Created
August 20, 2011 12:36
-
-
Save Reinmar/1159049 to your computer and use it in GitHub Desktop.
Hoisting, function declarations vs function expressions
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
console.log('--------- Hoisting:'); | |
(function () { | |
//console.log('3.1:', a); // -> ReferenceError | |
console.log('3.2:', b); // -> undefined | |
var b = 1; | |
console.log('3.3:', c); // -> undefined | |
if (false) { | |
var c = 1; | |
} | |
console.log('3.4:', c); // -> undefined | |
var d = 10; | |
(function () { | |
if (d === 10) { | |
var d = 20; | |
} | |
console.log('3.5:', d); // -> undefined | |
})(); | |
})(); | |
console.log('--------- FD vs FE:'); | |
(function () { | |
a('4.1:'); // -> 2 | |
var a = function (i) { | |
console.log(i, '1'); | |
}; | |
function a(i) { | |
console.log(i, '2'); | |
} | |
a('4.2:'); // -> 1 | |
function b() { | |
console.log('4.3: 1'); | |
} | |
function b() { | |
console.log('4.4: 2'); | |
} | |
b(); // -> 2 | |
var c1 = function c2(i) { | |
console.log(i, c1, c2); | |
}; | |
c1('4.5:'); // -> function, function | |
//c2('4.6:'); // -> ReferenceError | |
})(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment