function hoisting like var

Ingvar von Schoultz ingvar-v-s at comhem.se
Sun Jul 27 19:44:44 PDT 2008



Igor Bukanov wrote:
> 2008/7/28 Ingvar von Schoultz <ingvar-v-s at comhem.se>:
>> {{ }} is just the same as a scoping function used on ES3:
>>
>>    (function() { code })()
> 
> As Lars Hansen has pointed out any proposal for a shorthand for
> (function() { code })() has to deal with break/continue/return inside
> the code.

Indeed I wasn't thinking of that in my description.

> This is the the reason if anything I would prefer in ES3.1
> just shorthands for function definitions like
> 
> function() expr  - equivalent to function () { return expr; }. This is
> already in ES4 and is implemented by at least on implementation
> (SpiderMonkey).

I rarely feel a need for such tiny scoping blocks, but often
long for big scoping blocks with intuitive syntax.

> function optional_name { code } - equivalent to function
> optional_name() { code } - a hypothetical shortcut that would allow to
> write a pseudo-blocks like
> 
> function {
> }();
> 
> with clear emphasis that this is a lambda with usual rules for
> break/continue/return.

Yes, we can't have syntax that looks like a block but forbids
or misunderstands break/continue/return.

In theory {{ code }} could be converted to a function that
returns information about whatever break/continue/return was
reached. But I'd be quite surprised if that is easy.

Something like this. Written with syntax sugar:

     Outer:
     for (var OutName in OutThing)
         for (var InName in InThing)
         {{
             break Outer;
         }}

Translation:

     Outer:
     for (var OutName in OutThing)
     {
         var _Result = (function (_InThing)
         {
             for (var InName in _InThing)
             {
                 return ({JumpSpot: 'break Outer'})
             }
         })(InThing);
         if (_Result.JumpSpot == 'break Outer')
             break Outer;
     }

The inner for() is part of the scoping block, so it belongs
inside, even though the original code has it above.

We must make sure the value of InThing is available inside
even if the name is declared for a different variable inside.

I use initial underscore to indicate something internal and
invisible.

It doesn't look complicated here! Unfortunately these things
have a terrible tendency to grow in complexity...

-- 
Ingvar von Schoultz

------- (My quirky use of capitals in code comes from my opinion that
reserved and predefined words should all start with lowercase, and
user-defined should all start with uppercase, because this will easily
and elegantly prevent a host of name-collision problems when things
like programming languages are upgraded with new labels.)



More information about the Es4-discuss mailing list