Function declarations in statements

Brendan Eich brendan at mozilla.org
Sat Mar 17 15:05:56 PDT 2007


On Mar 17, 2007, at 11:46 AM, Igor Bukanov wrote:

> On 17/03/07, Brendan Eich <brendan at mozilla.org> wrote:
>> On Mar 17, 2007, at 4:41 AM, Igor Bukanov wrote:
>> That's right -- var and function are not equivalent. You cannot
>> rewrite function f() {} as var f = function (){}.
>
> Hence the proposal: make function f() {} to be equivalent to
> var f = function() {}; f.name = "f" placed at the beginning of the
> block. This still allows a conditional function definitions while
> removing the discrepancy with vars.

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); }

which at run-time results in an error of the "even is not a function"  
kind.

/be




More information about the Es4-discuss mailing list