Function declarations in statements

Igor Bukanov igor at mir2.org
Sat Mar 17 20:11:35 PDT 2007


On 17/03/07, Brendan Eich <brendan at mozilla.org> wrote:
> That's an incompatible change that breaks forward calls:
>
> function odd(n) { return n == 0 ? false : even(n-1); }
> alert(odd(4));
> function even(n) { return n == 0 ? true : odd(n-1); }
>
> Translating to var form, ignoring intrinsic name, results in:
>
> var odd = function (n) { return n == 0 ? false : even(n-1); }
> alert(odd(4));
> var even = function (n) { return n == 0 ? true : odd(n-1); }

No, the translated code is:
var odd = function (n) { return n == 0 ? false : even(n-1); }
var even = function (n) { return n == 0 ? true : odd(n-1); }
alert(odd(4));

since the idea for "function f() {}" at any place in the block to mean
"var f = function f() {}" at the beginning of the block. For top-level
function statements this is exactly what is ES3 requires, but it also
makes a consistent rules for functions declared inside blocks.

Regards, Igor



More information about the Es4-discuss mailing list