JS:为什么函数声明顺序在条件块内很重要?

JS: Why does function declaration order matter when inside a conditional block?

a();
function a() {
    $('.doit').text('Text was replaced (a)');   
}

if ($('.doit2').length) {
    b();
    function b() {
         $('.doit2').text('Text was replaced (b)');   
    }
}

a()调用正确,而b给出错误"b is not defined"。为什么?

(我读过关于提升但 function b 被声明,而不是变量。或者我错了吗?)

参见 fiddle - Firefox 出现错误,而 Chrome 正常。

根据 Javascript 规范,函数声明不允许在条件语句(或任何其他块)中使用。因此,这在技术上是未定义的行为。一些浏览器试图创建一个合理的行为。但是,您不应依赖浏览器能够正确解释这一点。

FunctionDeclarations are only allowed to appear in Program or FunctionBody. Syntactically, they can not appear in Block ({ ... }) — such as that of if, while or for statements.

http://kangax.github.io/nfe/#function-declarations-in-blocks