简体   繁体   中英

firebug console not doing hoisting

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

From above code, i will expect "hello" (and some undefined s) 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的工具提示

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM