return when desugaring to closures

Neil Mix nmix at
Tue Oct 14 07:38:43 PDT 2008

On Oct 13, 2008, at 6:39 PM, Brendan Eich wrote:

> Users may be modeling closures as capturing bindings, not scope chains
> of mutable objects, one per for (let...) statement or explicitly
> braced block. If so, could we make let declaration capture this way?
> Again, I'm proceeding from real users' complaints, not idle wishes.

Are you suggesting that closures over let capture bindings in the  
general case?  I hope not.  I think for loops are a special case,  
otherwise let is not the new var.  ;)

WRT for loops, it's important to remember that let provides an  
alternative that wasn't possible with var.  Suppose for moment we do  
*not* rebind on every iteration:

   for (let i = 0; i < objects.length; i++) {
     let j = i;
     objects[i].callback = function() {

The syntactic overhead for such a workaround is much less than for var:

   for (var i = 0; i < objects.length; i++) {
     objects[i].callback = buildCallback(i);

   function buildCallback(i) {

The for/closures "bug" is definitely a newbie trap, but its pain is  
not its discovery, but the difficulty of working around it.  To me  
this could be a winning argument against re-binding on each loop,  
since re-binding precludes the (admittedly dubious) use-case of  
updating inside the body:

for (let i = 0; i < 100; i++) {
   if (skipAhead) {
     i += 9;

More information about the Es-discuss mailing list