Javascript function scope discrepncy between chrome and firefox, which one is correct
我看到在Chrome和Firefox上执行的以下Javascript代码有不同的行为:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | var outer = function (param) { var inner = function (theinput) { return theinput * 2; }; return 'The result is ' + inner(param); }; console.log(outer(2)); console.log(inner(5)); Result on Firefox: "The result is 4" 10 Result on Chrome: "The result is 4" undefined |
由于inner被声明为"var",我猜Chrome上的输出是正确的,因为函数"inner"充当私有函数。 我认为firefox的实现是不正确的。
如果内部删除了"var",则内部函数在首次执行后将被提升为全局函数。我的理解是否正确?
Since inner is declared as"var", I guess the output on Chrome is correct as the function"inner" acts as a private function. I think firefox implementation is incorrect.
两者都不正确。 由于
这是我在Firefox和Chrome中测试代码时的行为。
是(除非您在严格模式下运行代码,在这种情况下,当您尝试将值分配给