VK said:
It wouldn't give this effect: parenthesis simply define *operator
execution precedence* like (1+2)*3
Did you try it?
But you are right. Parentheses are used to override precendence of
operators in expressions.
And that's the point. By adding them, the result becomes an expression,
not a function declaration. Function expressions does not expose
their function names to the surrounding scope, and since the value
isn't captured anywhere either, there is no reference to the function
object created.
(function dummy(args){}); means "execute function definition first,
then the rest". As the "rest" is not presented, parenthesis usage here
is not really necessary.
It means "evaluate function expression". Had they not been there, the
parser would have parsed it as a function declaration, an entirely
different syntactic category, with quite a different semantics.
Function "dummy" is still properly parsed, allocated and *referenced*.
No reference is created to the anonymous (well, anonymous on the
outside) function. It is free to be garbage collected.
Had it been a function declaration, it would have created a property
of the variable object of the same name, referencing the function
object.
For some reason it breaks though the proper execution on FF and (as it
seems) on Opera. They don't see "dummy" if parenthesis are used. There
is nothing in ECMAScript specifications to endorse it,
Oh, absolutely. You don't think I came up with the idea myself, do you?
But for completeness:
FunctionDeclaration and FunctionExpression are defined in section 13.
Checking the semantics, you can see that only a FunctionDeclaration
adds a property to the current variable object, whereas
FunctionExpression with an identifier creates a new level on the scope
chain to hold the identifier, one that is only visible for the body
of the function, not outside. This is spelled out in the "NOTE".
so it must be a bug to report (if it's not reported yet).
No, it's behavior consistent with the ECMAScript standard.
Typically, when Gecko and Opera agree on something, and IE doesn't,
it's a safe bet that it's IE that isn't in compliance with the
standard.
.... as usual, when properly is defined as "what IE does"
Something like:
function f() {
alert(foo); // undefined
Uninitialized, but already existing! Had you written "alert(bar)", you
would have gotten an exception for using a non-existing variable.
var foo = 'bar';
alert(foo); // 'bar'
}
It is as if all variable declarations (i.e., the "var variablename")
in the function body were at the top of it. Assignments to the
variables, including those part of a variable declaration statement,
are executed in the order they are reached. So, the above function
is equivalent to:
function f() {
var foo;
alert(foo);
foo = 'bar';
alert(foo);
}
or even
function f() {
alert(foo);
foo = 'bar';
alert(foo);
var foo;
}
/L