Function hoisting

Brendan Eich brendan at
Sat Jul 19 10:59:25 PDT 2008

On Jul 19, 2008, at 9:51 AM, Brendan Eich wrote:

>> 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).

Note also how this neatly separates duties with respect to const vs.  
var/let. If you want a const binding to the function that survives  
the end of block, you can do it in ES4, where const is assign-once:

const x;
if (truthy) {
     x = function () 1;
} else {
     x = function () 2;
// use x here

There is no free lunch, though. A typed function would probably want  
the signature restated as an annotation on the const. And of course  
if you want the function to have an intrinsic name, you'll need named  
function expressions above, which may redundantly restate "x" as the  
name of the function (as well as of the binding).


More information about the Es4-discuss mailing list