On 24/05/2005 12:22, (e-mail address removed) wrote:
[snip]
if (1==2) {
function invisible() {
alert("invisible() called");
}
}
This is a syntax error. I imagine it is not flagged because it is
considered too common, and will break too many scripts. However, it is a
mistake and should never occur within a script.
There are three ways to create a function object in ECMAScript: the
function declaration, the function expression, and the Function
constructor function.
The first, the function declaration, is fairly simple and recognisable:
function <identifier> ( <argument list> ) {
<function body>
}
What is not always appreciated is that function declarations can only
appear in two general locations: at the program (aka root, or global)
level, and within the function body of another function. So,
/* Program */
function myFunction() {
}
and
/* Program */
function myOuterFunction() {
/* Function body */
function myInnerFunction() {
}
}
are fine. However,
if(condition) {
/* Statement list */
function myFunction() {
}
}
is not because statements like if and while expect other statements.
Function declarations are somewhat special. Unlike most source elements,
they are evaluated before execution would normally reach them. This is
why you can call a function before you have defined it.
When a script is initially parsed, all function declarations at the
program level will be evaluated, and the corresponding function objects
will be created. It is only after this process that execution begins.
Similarly, when a function is called, any inner function declarations
(such as myInnerFunction, above) are evaluated before execution of that
function commences.
The second, the function expression, is a little awkward to explain,
mainly because it might be difficult to see the distinction with
function declarations. Essentially, a function expression works like any
other expression: it is not evaluated until execution reaches it. This
allows you to create functions conditionally:
var func;
if(condition) {
func = function( <argument list> ) {
<function body>
};
}
Only if condition is true will the expression be evaluated. If condition
is false, func will remain undefined.
So, how /do/ you distinguish between a function expression and a
declarations? Essentially, it depends on the beginning of a source
element. If it starts with a 'function' token, it is a declaration. If
it starts with something else, it's a function expression. For example,
(function() {
/* ... */
})();
is a function expression because the source element begins with an
opening parenthesis.
A function expression can still have an identifier
func = function <identifier>( <argument list> ) {
<function body>
};
but this is only so the function can refer to itself - for recursion,
for example. It's not to be used outside of the function body.
I'll ignore the Function constructor for now. I'm sure the above is more
than enough information. What I will say is that although the Function
constructor can be used to create function objects at runtime like
function expressions, there are major differences between the two.
[snip]
If something needs more explanation, please ask.
Mike