From:  "David Andersson" <>
Date:  17 Dec 2006 01:09:15 Hong Kong Time

Re: Function declaration as last expression in a script


On Nov 7, 8:34 pm, Mark Lundquist  wrote:
> Why is that?  Is it a bug?  If the function declaration is an expression
> and yields a value that can be assigned to foo in the first script, why
> doesn't the second script return that value as the last expression
> evaluated?

First case is a function expression - A function literal that returns
the function object generated.
You then reference the variable you stored that function object in.

The second case is a function declaration. Function declarations are
NOT expressions and thus do not return a value. As an aside, it can be
noted that the second case even happen to be a syntax error that rhino
evidently fails to catch. The function name is optional in function
expressions, but required in function declarations. The generated
function object is to be assigned to the specified function name, so
without a function name the declaration is useless.

To use a function declaration in a way that is roughly equivalent to
your first example:

    function foo(){}

This should have the same effects as your first example, except for the
fact this binds on the parsing pass over the code, and your first
example binds on the execution pass over the code.

That the function declaration doesn't return a function object is the
specified behaviour.
That no syntax error is raised, however, is a bug.
David "liorean" Andersson