Consider the following JavaScript code:
(function(){
foo();
function foo(){ alert('Hello, World!'); }
})();
In Firefox, Opera, and Chrome, this behaves as expected; we get a warning. Now in contrast:
(function(){
if (true){
foo();
function foo(){ alert('Hello, World!'); }
}
})();
Firefox 3.6 and 4 (beta) (i.e. SpiderMonkey for both) throws an exception: foo is undefined
Chrome (i.e.V8) and Opera (i.e. any Opera engine) work as expected (for me).
What is the right behavior, or is it left for implementation to solve?
FWIW, re-enable it in the function, and again it's good to return to FF:
(function(){
if (true){
(function(){
foo();
function foo(){ alert('Hello, World!'); }
})();
}
})();
source
share