Function declarations in statements

liorean liorean at gmail.com
Sun Mar 18 06:05:21 PDT 2007


On 18/03/07, Brendan Eich <brendan at mozilla.org> wrote:
> Backward compatibility is required here, since script on the web declares
> functions with names that replace pre-defined properties that may not have
> the right default attributes (in particular, may be ReadOnly and
> DontDelete).

The way I see this working, function statements would create a
property on the variable object of the surrounding scope, initialise
to undefined just like var, and carry on. When execution reaches the
block in which the function statement was declared, the function
object is assigned to that variable and the appropriate attributes are
set.

> This part, I agree is an improvement on the function statement rule in
> SpiderMonkey and Rhino. ES4 already has |let function f(...)...| for
> lexically scoped block-local function binding (hoisted to top of block). It
> sounds like your proposal is the same as let function provided the function
> statement is enclosed in a block. With let, a binding such as
[snip]
> binds x in the enclosing block, not in an implicit block around the if's
> then clause.

The proposal still binds in the surrounding function scope, not in
block scope. And it creates the variable when entering that scope. It
just delays the actual assignment of the function object to that
variable till control flow has reached the block containing the
function statement.

Though I think the same should be happening to all statements that can
contain other statements, not just block statements. For example,

    function fn(){return'before if-statement'}
    if(true)
        function fn(){return'then-clause'}

In this case, I expect the 'before if-statement' function to be
handled exactly per the ES3 rules. When execution reaches the
if-statement, that variable is assigned the 'then-clause' function
object. In the case that the nested statement is a block statement,
like this:

    function fn(){return'before if-statement'}
    if(true){
        function fn(){return'first'}
        print(fn())
        function fn(){return'second'}
    }

The function objects are always assigned to the variable before
anything else in the block statement, so the text displayed will be
"second".
-- 
David "liorean" Andersson



More information about the Es4-discuss mailing list