Function declarations in statements

liorean liorean at
Fri Mar 16 21:36:32 PDT 2007

On 17/03/07, Yuh-Ruey Chen <maian330 at> wrote:
> You're right, expression statements that start with "function" are
> illegal in ES3. But as you've pointed out, some popular ES3
> implementations allow function declarations in these contexts and
> compile-time initialize them. The most significant problem with making
> these run-time initialized is the usage-before-declaration feature. I've
> seen a few cases where a function is called or referenced before it's
> declared in an expression statement (tho I can't provide any real world
> examples - I'm not working in web development anymore). Definitely bad
> practice, but bad practice exists.

Well, bad practice that doesn't work in Mozilla or Safari. The code
I've seen that assumes declaration before use in statements is all of
the other kind - it assumes that if a function with the same name is
declared in both if and else paths of a conditional, the version will
be selected according to the path taken. But as I said, if you spider
for it you will find very few examples of code relying on either
behaviour. For some types of statements, none at all. (Labelled
statements being one example which has an already very low usage,
before you count this behaviour in.)

> I would actually prefer such functions to be run-time initialized, but I
> fear there are some existing programs where it can produce runtime
> errors in a ES4 context. That by itself is bad, but even worse, some
> programs won't even cause exceptions to be thrown.

And all those programs will not work in Mozilla or Safari.

> BTW, function expressions _are_ useful in expression statements - just
> consider the pseudo-block-scope idiom: (function(){...}()).

Which is an expression statement being a parenthesised expression
containing a call expression containing a function expression. The
call expression uses the return from a function expression. An
expression statement being a function expression, on the other hand,
is totally useless because no reference to the function object will
survive to be called or otherwise used. But then that is not allowed
in the syntax, either.
David "liorean" Andersson

More information about the Es4-discuss mailing list