There are two things happening in that code, one of which is specified behavior, and the other which is syntactically invalid (for now) and the results of which will vary by JavaScript engine.
The invalid bit is that you can't have a function declaration within a conditional block. E.g., this bit is invalid:
if (false) {
function fn2() {
return "fn2 if --> false"
}
}
Some engines will treat it as a function declaration, meaning it's not subject to the flow of the step-by-step code (because function declarations aren't, they happen before the step-by-step flow).
Other engines will (in effect) rewrite that as a function expression for you, putting it in the step-by-step flow.
I believe ECMAScript6 will be addressing this.
The specified bit relates to just having two declarations within the same scope, e.g.:
var fn1 = function() {
function fn2() {
return "the first fn2"
}
return fn2();
function fn2() {
return "the second fn2"
}
};
fn1(); // "the second fn2"
The specification clearly states that all function declarations within a scope are processed in order of the source code, and so the above (with the invalid bit removed) reliably uses the second fn2
, not the first.