Array - What am I doing wrong?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Lasse Reichstein Nielsen

    #16
    Re: Array - What am I doing wrong?

    "Douglas Crockford" <nospam@covad.n et> writes:
    [color=blue][color=green][color=darkred]
    >> >I hope you can prove me mistaken on this one: Function declarations
    >> >are only allowed at the top level of programs or function bodies.[/color][/color]
    >
    > Whoa. Hold the phone. That is not true.[/color]

    I hope so, but I can't convince myself by looking at the ECMAScript
    specification (December 1999 version, freshly downloaded from ECMA's
    homepage[1]). I can, however, convince myself of the opposite.
    [color=blue]
    > Function statements can appear anywhere that a statement can
    > appear.[/color]

    FunctionDeclara tion can only be produced by SourceElement.
    SourceElement can only be produced by SourceElements.
    SourceElements can only be produced by Program or FunctionBody.

    The productions are:
    ---
    FunctionBody : See clause 13
    SourceElements

    Program : See clause 14
    SourceElements

    SourceElements : See clause 14
    SourceElement
    SourceElements SourceElement
    SourceElement : See clause 14
    Statement
    FunctionDeclara tion
    ---

    That means that a function declaration can only occur directly
    inside a function body or a program.

    Statments in general can also occur inside a block:
    ---
    Block : See 12.1
    { StatementList_o pt }
    StatementList : See 12.1
    Statement
    StatementList Statement
    ---
    FunctionDeclara tions can not appear here, or most other
    places where statements can.

    FunctionExpress ion can appear almost anywhere another expression can,
    except at the beginning of an ExpressionState ment (yes, you can wrap
    it in parenthese, but if you *don't*, it is not correct - in the
    example, I didn't).
    [color=blue]
    > And function expressions can appear pretty much anywhere.[/color]

    Can you show me a derivation of
    if (true) {
    function foo(){alert();}
    foo();
    }
    in the ECMAScript syntax?

    I added to "foo()" to suggest that browsers treat it as a function
    declaration. If it is a function expression, then the scope of "foo"
    would only be the body of the function. It isn't in Opera 7, IE 6,
    Mozilla FB 0.7 or Netscape 4 (the browsers I have tested).

    In fact, Opera and IE also accept and run:
    if (true) {
    foo();
    function foo(){alert();}
    }
    while Mozilla FB and Netscape 4 says that "foo is not defined."
    [color=blue]
    > It is syntactically correct.[/color]

    Prove it. :)
    [color=blue]
    > Not only that,
    >
    > (function (){ })();
    >
    > is correct, too.[/color]

    Absolutely, that is a function expression, and the parenthesis at the
    beginning makes it a legal expressionState ment. It is a completely
    different beast.

    Instead of the function declaration:
    function foo(){...}
    you can write the variable statement:
    var foo = function foo(){...};
    The only difference is that the function declaration should be
    processed before any statements in the same scope are executed,
    so you can use a function that is declared later in the code.

    /L
    [1]<URL:http://www.ecma-international.o rg/publications/standards/Ecma-262.htm>
    --
    Lasse Reichstein Nielsen - lrn@hotpop.com
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    Working...