Function declarations in statements

Yuh-Ruey Chen maian330 at
Fri Mar 16 21:09:41 PDT 2007

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.

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.

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

-Yuh-Ruey Chen

liorean wrote:
> In what way is it backwards incompatible?
> It's a syntax error in ES3. Function declarations are only allowed in
> source elements context. Function expressions are not allowed in
> expression statements (indeed, they would be useless there, since the
> only way you can get a reference to a function expression is by it's
> return value. The name is bound in local scope only.). Thus we're
> speaking of defining behaviour for an area that was not covered in any
> way by ES3.
> All you will be doing is adding a missing a feature, you won't be
> breaking any ES3 code. And I think if you spider the web, there will
> be very few, or none, scripts relying on either the SpiderMonkey or
> the JScript behaviour.
> Further, Microsoft will have to split up their currently unified
> handling of all types of functions in JScript if they want to fully
> comply to ES3 in this. Changing the FunDeclInSmt handling would be
> easy to do at the same time.

More information about the Es4-discuss mailing list