简体   繁体   English

firebug控制台没有吊装

[英]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. 从上面的代码,我将期望在控制台上记录"hello" (和一些undefined s)。 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). 通过MDN (这里涉及的很多不是标准的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 . 第一个将警告42,而第二个将抛出ReferenceError

And here is the code that gets executed when you are playing with Firebug: 以下是使用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 . 观察到的行为似乎是Firefox javascript引擎中的一个小故障,因为在chrome和IE9中没有观察到它,看到这个小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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