Function hoisting

Brendan Eich brendan at mozilla.org
Sat Jul 19 09:51:44 PDT 2008


On Jul 19, 2008, at 7:20 AM, Neil Mix wrote:

> On Jul 19, 2008, at 12:25 AM, Brendan Eich wrote:
>
>> * ES3.1 and ES4 will both allow ‘function’ inside control flow
>> statements [directly inside an explicit block, not as the lone
>> unbraced consequent statement of an if, while, etc. /be], and it
>> will be hoisted to the top of the block and initialized on block
>> entry (to be compatible with how functions behave in ES3)
>
>
> What's the behavior of an if/else control flow statement that contains
> a function definition in each explicit block?
>
> if (true) {
> 	function x() {
> 		return 1;
> 	}
> } else {
> 	function x() {
> 		return 2;
> 	}
> }

Block scope -- an x in each branch's block. This avoids capturing  
problems. To make a binding that extends beyond either block one just  
uses let or var in an outer scope, and assigns. Richard Cornford made  
this point in a different context last week (about memoizing top  
level functions via var, not by overwriting a function definition).

/be




More information about the Es4-discuss mailing list