On 01.01.2011 2:08, Garrett Smith wrote:
On 12/31/10, Marc Harter<[email protected]> wrote:
The thing is that (almost?) all explanations (I repeat -- books, articles,
etc) talked about `calling`, that is wrong. While the reason is that
parentheses at the end are not parentheses of a call (`Arguments`
production), but just a grouping operator (without an expression inside)
which causes the error.
function foo(x) {
alert(x)
}(1);
There is no error. But it's not a call. It's a FD and the following
grouping operator with the expression `1`. I.e. it's:
function foo(x) {
alert(x)
}
(1);
So the parses treats these as two distinct statements, that is why a
semicolon is not automatically inserted after a FD?
And since an empty grouping operator () causes an error, exactly this is
the reason:
function foo(x) {
alert(x)
}();
and not in "FD cannot be _called_", since it's not about calling at all.
This was new to me, I had read or perhaps just assumed it was a call on a FD
prior, thanks for clearing that up.
Remember, a A FunctionDeclaration's function is added to the
VariableEnvironment in Variable Instantiation. It is not and cannot be
an ExpressionStatement.
An FunctionExpression is an ExpressionStatement; it is not bound to
the VE (VariableEnvironment) on entering an execution context. A
FunctionExpression is disabiguated from a FunctionDeclaration by the
fact that an ExpressionStatement can't start with the `function`
keyword. From ES5:
| 12.4 Expression Statement
| Syntax
|
| ExpressionStatement :
| [ lookahead ∉ { {, function }] Expression
| NOTE An ExpressionStatement cannot start with an opening curly
| brace because that might make it ambiguous with a Block. Also, an
| ExpressionStatement cannot start with the function keyword because
| that might make it ambiguous with a FunctionDeclaration.
Have a look at some source code:
function f() {
function g() {} //1
function h(v) {alert(v);}(2); // 2
+function k(v){alert(v);}(3); // 3
// JScript NFE bug.
alert(typeof k);
}
// debugger;
f();
When `f` is called, a VariableEnvironment (VE) is created. For each
FunctionDeclaration in the FunctionBody of `f` the Identifier of that
FD is added to the VE.
Which Identifiers above must be added to the VE and which must not?
The divergent result seen in the most widespread versions of JScript
is explained in MS-ES3. I cited that two days prior, in response to
Ben Alman's post about the old JScript `catch` block Identifier bug
http://www.mail-archive.com/[email protected]/msg00616.html
"In JScript Variable Instantiation, identifiers from catch blocks and
FunctionExpressions are added to the VO."
(VO is short for the ES3 term "VariableObject", called
"VariableEnvironment" in ES5).
An example and explanation of JScript's `catch` scope bug can be seen
here: http://bclary.com/log/2004/09/26/boot-camp-javascript#Example_Try_Catch
While all explained above is correct, it's not about why there is an
error if not to surround a function into parentheses. Now it should be
clear that it's not about "starting with a `function` keyword". I
mentioned this fact (about "starting with a `function` keyword") to
underline that in the global scope it's a function declaration (FD) and
has nothing with a expression statement.
Dmitry.
--
To view archived discussions from the original JSMentors Mailman list:
http://www.mail-archive.com/[email protected]/
To search via a non-Google archive, visit here:
http://www.mail-archive.com/[email protected]/
To unsubscribe from this group, send email to
[email protected]