Вопрос

console.log(a());
function a(){
    console.log("hello");
}

From above code, i will expect "hello" (and some undefineds) to be logged on console. But firebug gives

ReferenceError: a is not defined

So firebug does not do hoisting?

Это было полезно?

Решение

The reason for the issue is that

functions do not hoist when declared inside a child block.

by MDN (Much covered here is not standard ECMAScript).

Compare the following snippets:

alert(c());
function c(){return 42;}

and

{
    alert(c());
    function c(){return 42;}
}

The first one will alert 42, whereas the second one will throw ReferenceError.

And here is the code that gets executed when you are playing with Firebug: Firebug's tooltip

data;
with(_FirebugCommandLine){ // >> block begins
    console.log(a());
    function a(){
        console.log("hello");
    }
} // << block ends

Update
The behavior observed seems to be a glitch in Firefox javascript engine because it is not observed in chrome and IE9, see this fiddle.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top