console.log(a());
function a(){
console.log("hello");
}
从上面的代码,我期望"hello"
(还有一些undefined
s) 登录控制台。但是萤火虫给出了
ReferenceError: a is not defined
那么firebug不做提升吗?
问题的原因在于
在子块内声明时,函数不会提升。
by MDN https://developer-new.mozilla.org/en-US/docs/JavaScript/Reference/Scope_Cheatsheet#function_oddities(这里涵盖的大部分内容都不是标准的 ECMAScript)。
比较以下片段:
alert(c());
function c(){return 42;}
and
{
alert(c());
function c(){return 42;}
}
第一个将警告 42,而第二个将抛出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
观察到的行为似乎是 Firefox javascript 引擎中的一个故障,因为在 chrome 和 IE9 中没有观察到它,请参阅此fiddle http://jsfiddle.net/FZ92S/.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)