(function(){ //运行正常 test1(); function test1() { console.log('123'); }; })() (function(){ //出错,test2未定义
test2(); test2 = function () { console.log('123'); }; })()
以下摘录自《Javascript: the good parts》
/* function statements are subject to hoisting. This means that regardless of where a function is placed, it is moved to the top of the scope in which it is defined. This relaxes the requirement that functions should be declared before used, which I think leads to sloppiness. It also prohibits the use of function statements in if statements. It turns out that most browsers allow function statements in if statements, but they vary in how that should be interpreted. That creates portability problems. */