From: Dmitry A. Soshnikov on
On May 24, 11:45 am, Garrett Smith <dhtmlkitc...(a)gmail.com> wrote:
> On 5/23/2010 4:00 PM, FAQ server wrote:
>
> > -----------------------------------------------------------------------
> > FAQ Topic - What is a function statement?
> > -----------------------------------------------------------------------
>
> > The term function statement has been widely and wrongly used to
> > describe a ` FunctionDeclaration `.
>
> New FAQ Topic idea:
>
> | What is a FunctionDeclaration?
>
> ?

There should be described the general points of this type of
functions:

<http://dmitrysoshnikov.com/ecmascript/chapter-5-functions/#function-
declaration>

To avoid some technical terms (because this is a FAQ, but not a deep
article), it can be described as:

This is a function which:

- always has a name;
- is created before the code execution;
- available for an execution via its name before and after its
definition in the source code position (in contrast see <link
FunctionExpression />);
- can appear in the source code position directly either at /Program/
level or in the FunctionBody of another function (in any other
position FunctionDeclaration cannot appear, i.e. it is impossible to
define it in the expression or statement position; in contrast see
some implementations extension <link FunctionStatement />).

P.S.: in addition, it is a bit ugly to use BNF non-terminals such as
<FunctionStatement> and other. They are related only with lexical
grammar lexers/parsers, but not with human reading. I prefer a
<Function Expression> instead.

Dmitry.
From: Ry Nohryb on
On May 24, 1:00 am, "FAQ server" <javascr...(a)dotinternet.be> wrote:
> -----------------------------------------------------------------------
> FAQ Topic - What is a function statement?
> -----------------------------------------------------------------------
>
> The term function statement has been widely and wrongly used to
> describe a ` FunctionDeclaration `. This is misleading because in
> ECMAScript, a ` FunctionDeclaration ` cannot appear as a Statement.

You're misleading, and your screwed-up FAQ too:

12.5: The if Statement: Syntax: if ( Expression ) Statement

javascript: f(); if (0) function f () { alert("Declaration, Smith, DE-
CLA-RA-TION") };

Safari, Chrome, Opera, IE: "Declaration, Smith, DE-CLA-RA-TION"
Mozillas: TypeError: f is not a function.

12.1 Block: Syntax: { StatementList }

javascript: { f(); function f () { alert("Declaration, Smith, DE-CLA-
RA-TION") }; }

Safari, Chrome, Opera, IE: "Declaration, Smith, DE-CLA-RA-TION"
Mozillas: TypeError: f is not a function.

12.6 Iteration Statements: Syntax: do Statement while(Expression);

javascript: f(); do function f () { alert("Declaration, Smith, DE-CLA-
RA-TION") } while (0);

Safari, Chrome, Opera, IE: "Declaration, Smith, DE-CLA-RA-TION"
Mozillas: TypeError: f is not a function.

12.6 Iteration Statements: Syntax: while ( Expression ) Statement

javascript: f(); while (0) function f () { alert("Declaration, Smith,
DE-CLA-RA-TION") };

Safari, Chrome, Opera, IE: "Declaration, Smith, DE-CLA-RA-TION"
Mozillas: TypeError: f is not a function.

12.6 Iteration Statements: Syntax: for (Expression; Expression ;
Expression) Statement

javascript: f(); for (;false;) function f () { alert("Declaration,
Smith, DE-CLA-RA-TION") }

Safari, Chrome, Opera, IE: "Declaration, Smith, DE-CLA-RA-TION"
Mozillas: TypeError: f is not a function.

etc, etc.

> To add to this confusion, some implementations, notably Mozillas', provide
> a syntax extension called function statement. This is allowed under
> section 16 of ECMA-262, Editions 3 and 5.

To your confusion, might be. The whole thing, reworded, could end up
being true. But the way you've got it worded now, it isn't.
--
Jorge.
From: Dmitry A. Soshnikov on
On 24.05.2010 16:44, Ry Nohryb wrote:
> On May 24, 1:00 am, "FAQ server"<javascr...(a)dotinternet.be> wrote:
>> -----------------------------------------------------------------------
>> FAQ Topic - What is a function statement?
>> -----------------------------------------------------------------------
>>
>> The term function statement has been widely and wrongly used to
>> describe a ` FunctionDeclaration `. This is misleading because in
>> ECMAScript, a ` FunctionDeclaration ` cannot appear as a Statement.
>
> You're misleading, and your screwed-up FAQ too:
>
> 12.5: The if Statement: Syntax: if ( Expression ) Statement
>
> javascript: f(); if (0) function f () { alert("Declaration, Smith, DE-
> CLA-RA-TION") };
>
> Safari, Chrome, Opera, IE: "Declaration, Smith, DE-CLA-RA-TION"
> Mozillas: TypeError: f is not a function.
>
> 12.1 Block: Syntax: { StatementList }
>
> javascript: { f(); function f () { alert("Declaration, Smith, DE-CLA-
> RA-TION") }; }
>
> Safari, Chrome, Opera, IE: "Declaration, Smith, DE-CLA-RA-TION"
> Mozillas: TypeError: f is not a function.
>
> 12.6 Iteration Statements: Syntax: do Statement while(Expression);
>
> javascript: f(); do function f () { alert("Declaration, Smith, DE-CLA-
> RA-TION") } while (0);
>
> Safari, Chrome, Opera, IE: "Declaration, Smith, DE-CLA-RA-TION"
> Mozillas: TypeError: f is not a function.
>
> 12.6 Iteration Statements: Syntax: while ( Expression ) Statement
>
> javascript: f(); while (0) function f () { alert("Declaration, Smith,
> DE-CLA-RA-TION") };
>
> Safari, Chrome, Opera, IE: "Declaration, Smith, DE-CLA-RA-TION"
> Mozillas: TypeError: f is not a function.
>
> 12.6 Iteration Statements: Syntax: for (Expression; Expression ;
> Expression) Statement
>
> javascript: f(); for (;false;) function f () { alert("Declaration,
> Smith, DE-CLA-RA-TION") }
>
> Safari, Chrome, Opera, IE: "Declaration, Smith, DE-CLA-RA-TION"
> Mozillas: TypeError: f is not a function.
>
> etc, etc.
>

Well, all excluding Mozilla are wrong. Mozilla is right only because of
section 16 of the ECMA-262-3.

By the way, I have any idea why they didn't standardized Function
Statements in the 5th edition?

That strange phrase from the spec /"ExpressionStatement cannot start
with the *function* keyword because that might make it ambiguous with a
FunctionDeclaration"/ is really strange -- because how then Mozilla
distinguishes FD from FS? Easy I guess, by the context of the source
code position. For what to write it in the spec (including 5th edition),
if it is easy to distinguish? And I think for some it would be
convenient to define functions in the declaration view.

Such dynamically created (on condition at runtime) functions (exactly in
declaration view I mean) there are even in PHP.

Dmitry.
From: Richard Cornford on
On May 24, 2:35 pm, Dmitry A. Soshnikov wrote:
> On 24.05.2010 16:44, Ry Nohryb wrote:
>> On May 24, 1:00 am, FAQ server wrote:
>>> --------------------------------------------------------
>>> FAQ Topic - What is a function statement?
>>> --------------------------------------------------------
>
>>> The term function statement has been widely and wrongly
>>> used to describe a ` FunctionDeclaration `. This is
>>> misleading because in ECMAScript, a ` FunctionDeclaration
>>> ` cannot appear as a Statement.
>
>> You're misleading, and your screwed-up FAQ too:
<snip>
> Well, all excluding Mozilla are wrong.

All are right (to the extent that Ry Nohryb observed/demonstrated)
because in every case what they are doing can qualify as an extension
to ECMAScript.

> Mozilla is right only because of
> section 16 of the ECMA-262-3.
>
> By the way, I have any idea why they didn't standardized Function
> Statements in the 5th edition?

There was an attempt to move function declarations into the set of
Statements. When I looked at the proposed drafts at the time it was
clear that the work in doing that had hardly been started and what
they had would never work. But that was quite near their proposed
completion date for the new spec so it was probably easier to go back
to what had been there before than to resolve the issues trying to
make them a type of statement was going to bring up.

> That strange phrase from the spec /"ExpressionStatement cannot
> start with the *function* keyword because that might make it
> ambiguous with a FunctionDeclaration"/ is really strange --
> because how then Mozilla distinguishes FD from FS? Easy I
> guess, by the context of the source code position. For what
> to write it in the spec (including 5th edition), if it is easy
> to distinguish?

Have you noticed that section 5.1.1 (3rd Ed.) is entitle "Context-Free
Grammars"? It is quite a change to switch from context-free to context
dependent.

> And I think for some it would be convenient to
> define functions in the declaration view.
<snip>

How much difference in 'convenience' would there be? You can evaluate
a function expression conditionally and assign the result to a
variable, so whatever the job is it can be done with existing syntax.
So the difference in convenience is that between writing - x =
function(n){ ... }; - and - function x(n){ ... } -, which doesn't seem
that much.

Richard.
From: Ry Nohryb on
On May 24, 3:35 pm, "Dmitry A. Soshnikov" <dmitry.soshni...(a)gmail.com>
wrote:
> On 24.05.2010 16:44, Ry Nohryb wrote:
> > On May 24, 1:00 am, "FAQ server"<javascr...(a)dotinternet.be>  wrote:
>
> >> The term function statement has been widely and wrongly used to
> >> describe a ` FunctionDeclaration `. This is misleading because in
> >> ECMAScript, a ` FunctionDeclaration ` cannot appear as a Statement.
>
> > You're misleading, and your screwed-up FAQ too:
>
> > 12.5: The if Statement: Syntax: if ( Expression ) Statement
>
> > javascript: f(); if (0) function f () { alert("Declaration, Smith, DE-
> > CLA-RA-TION") };
>
> > Safari, Chrome, Opera, IE: "Declaration, Smith, DE-CLA-RA-TION"
> > Mozillas: TypeError: f is not a function.
>
> > (...)
>
> > etc, etc.
>
> Well, all excluding Mozilla are wrong.

No, no one but Smith is wrong : his statement is obviously false: "in
ECMAScript, a ` FunctionDeclaration ` cannot appear as a Statement" is
FALSE.

> Mozilla is right only because of section 16 of the ECMA-262-3.

All of them are fully compliant.
--
Jorge.
First  |  Prev  |  Next  |  Last
Pages: 1 2 3 4 5 6 7 8 9 10
Prev: IE problem with iframe reload
Next: IE's vertical scroll bar