Consider the following JavaScript code:
(function(){
foo();
function foo(){ alert('Hello, World!'); }
})();
In Firefox, Opera and Chrome, this behaves as expected; we get an alert. 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 not defined
Chrome (i.e. V8) and Opera (i.e. whatever engine Opera uses) work as expected (by me).
Which is the correct behaviour, or is this left to implementations to decide?
FWIW, wrap it up in a function again and it's good to go in FF again:
(function(){
if (true){
(function(){
foo();
function foo(){ alert('Hello, World!'); }
})();
}
})();