Firefox 对函数语句的解释不同,显然它们破坏了函数声明的声明提升。(关于命名函数/声明与表达式的好读物)
为什么 Firefox 对语句的解释不同是因为下面的代码:
if ( true ) {
function test(){alert("YAY");}
} else {
function test(){alert("FAIL");}
}
test(); // should alert FAIL
由于声明提升,函数test
应始终警告“失败”,但在 Firefox 中不会。上面的代码实际上在 Firefox 中警告“YAY”,我怀疑导致这种情况发生的代码最终完全破坏了声明提升。
我假设当函数声明位于 if/else 或 try/catch 语句中时,Firefox 会将函数声明转换为 var 声明。像这样:
// firefox interpretted code
var test; // hoisted
if (true) {
test = function(){alert("yay")}
} else {
test = function(){alert("fail")}
}
在与 Šime Vidas 进行了简短的辩论后,我不得不说 Firefox 处理函数声明是非标准的,因为:
生产 SourceElement:Statement 被处理为函数声明,不采取任何行动。
生产 SourceElement : 语句的评估如下:
- 评估声明。
- 返回结果(1)。
FunctionDeclaration 和 Statement 都是 SourceElements,因此,语句中不应该有 FunctionDeclarations(if/else,try/catch)。给 Šime Vidas 一块巧克力蛋糕!
Try/catch 基本上是另一种形式的 if/else 并且可能使用相同的异常代码。