function hoisting like var

Igor Bukanov igor at mir2.org
Fri Jul 25 05:36:16 PDT 2008


2008/7/25 Lars Hansen <lhansen at adobe.com>:
> Is
>
>  {{ return }}
>
> syntactic sugar for
>
>  (function() {{ return }})()
>
> too?

I have forgot about the return or break/continue. But this is not a
point since a syntax sugar for (function() { code })() can require
that code must not contain return or break/continue outside the code
effectively making it a let expression on steroids. The point is that
is it worth to provide a shortcut for (function() { code })() since
that is used on web. Of cause, if desired, such shortcut should be
pure syntax extension over ES3, not {{ }} as that silently changes the
meaning of the existing code.

One way to get that extension comes from an observation that ES4
allows to drop the  braces around function body and the return keyword
if the body is the single return exp. If ES3.1 would support it, then
to simulate an effect of let expression like

  let (a = arg1, b = arg2) expression

one could write

  (function(a, b) expression)(arg1, arg2)

or

  (function(a, b) a = arg1, b = arg2, expression)()

If, in addition to this shortcut, ES4 would allow to drop an empty
argument list from a function with a requirement that function { }
always means an expression and ES3.1 would also support that, then in
place of a block with let variables one can write:

function {

}();

Regards, Igor



More information about the Es4-discuss mailing list