萤火虫控制台不做吊装 [英] firebug console not doing hoisting

查看:66
本文介绍了萤火虫控制台不做吊装的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

从上面的代码中,我希望"hello"(和某些undefined)要登录到控制台.但是萤火虫给了

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?

推荐答案

问题的原因是

在子块中声明时,函数不会提升.

functions do not hoist when declared inside a child block.

MDN (涵盖了很多内容)这不是标准的ECMAScript).

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

比较以下片段:

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

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

第一个警报将发出42,而第二个警报将引发ReferenceError.

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

以下是在使用Firebug时执行的代码:

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

更新
观察到的行为似乎在Firefox JavaScript引擎中是一个小故障,因为在chrome和IE9中未观察到此行为,请参见此 fiddle a>.

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.

这篇关于萤火虫控制台不做吊装的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆