firebug console not doing hoisting

ぐ巨炮叔叔 提交于 2019-12-12 14:41:49

问题


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?


回答1:


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:

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.



来源:https://stackoverflow.com/questions/11693526/firebug-console-not-doing-hoisting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!