简体   繁体   English

在Chrome和Firefox中使用Javascript吊装

[英]Javascript Hoisting in Chrome And Firefox

Running this in Chrome and Firefox gives different answers: 在Chrome和Firefox中运行此功能会给出不同的答案:

(function() {

        if(true) {
            function f() { alert("yes"); };
        } else {
            function f() { alert("no"); };
        }
        f();

    })();

In Chrome the result is 'no' In Firefox the result is 'yes' 在Chrome中,结果是'不'在Firefox中,结果为'是'

Why the difference? 为什么不同?

Declaring functions inside conditional statements is non-standard, so do not do that . 在条件语句中声明函数是非标准的,所以不要这样做 That's a known issue. 这是一个众所周知的问题。 You may use function expressions instead of the declarations: 您可以使用函数表达式而不是声明:

var f;
if(true) {
    f = function() { alert("yes"); };
} else {
    f = function() { alert("no"); };
}
f();

The famous Kangax article on function expressions gives some additional details: 关于函数表达式着名Kangax文章提供了一些额外的细节:

FunctionDeclarations are only allowed to appear in Program or FunctionBody . FunctionDeclarations只允许出现在ProgramFunctionBody中 Syntactically, they can not appear in Block ({ ... }) — such as that of if , while or for statements. 从语法上讲,它们不能出现在Block ({ ... }) - 例如ifwhilefor语句。 This is because Blocks can only contain Statements , not SourceElements , which FunctionDeclaration is. 这是因为只能包含声明 ,不是SourceElements,FunctionDeclaration是。

The same article also says: 同一篇文章还说:

It's worth mentioning that as per specification, implementations are allowed to introduce syntax extensions (see section 16), yet still be fully conforming. 值得一提的是,根据规范,允许实现引入语法扩展 (参见第16节),但仍然完全符合要求。 This is exactly what happens in so many clients these days. 这正是现在这么多客户所发生的事情。 Some of them interpret function declarations in blocks as any other function declarations — simply hoisting them to the top of the enclosing scope; 其中一些将块中的函数声明解释为任何其他函数声明 - 只需将它们提升到封闭范围的顶部; Others — introduce different semantics and follow slightly more complex rules. 其他 - 引入不同的语义并遵循稍微复杂的规则。

From V8 (Chrome JavaScript engine) bug tracker : 来自V8(Chrome JavaScript引擎) 错误跟踪器

Not a bug. 不是错误。 Firefox is the only browser that does what you're expecting. Firefox是唯一能够满足您期望的浏览器。

The behavior of Safari and IE on this is the same as Chrome's/V8's. Safari和IE的行为与Chrome的/ V8相同。

This happens due to Firefox lack of function hoisting, as conceived in ECMAScript 5. 这是因为Firefox缺乏功能提升,正如ECMAScript 5中所设想的那样。

Chrome correctly assigns a value to f() before running the body of the function, so the first version of f() is overwritten by the second one. Chrome在运行函数体之前正确地为f()赋值,因此f()的第一个版本被第二个版本覆盖。

SpiderMonkey (Firefox's JavaScript engine) runs the code without pre-assignin a value to f(), so it uses the only value that encounters on its way: function f() { alert("yes"); }; SpiderMonkey(Firefox的JavaScript引擎)运行代码而不预先为f()赋值,因此它使用在其路上遇到的唯一值: function f() { alert("yes"); }; function f() { alert("yes"); };

what's function hoisting? 什么功能吊装?
JavaScript's function scope means that all variables declared within a function are visible throughout the body of the function. JavaScript的函数范围意味着函数内声明的所有变量在函数体中都是可见的。 Curiously, this means that variables are even visible before they are declared. 奇怪的是,这意味着变量在声明之前甚至可见。 This feature of JavaScript is informally known as hoisting: JavaScript code behaves as if all variable declarations in a function (but not any associated assignments) are “hoisted” to the top of the function. JavaScript的这个特性被非正式地称为提升:JavaScript代码的行为就好像函数中的所有变量声明(但不是任何相关的赋值)被“提升”到函数的顶部。

sources: 来源:
http://statichtml.com/2011/spidermonkey-function-hoisting.html http://statichtml.com/2011/spidermonkey-function-hoisting.html
2011 - o'reilly - javascript - the definitive guide 6th edition 2011 - o'reilly - javascript - 权威指南第6版

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

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