calling functions

C

Chris

Hello,

Calling a function always works when the invocation is placed AFTER
the function-definition.
But what if the invoke-statement is placed BEFORE it ?
I thought it shouldn't work but apparently ... in some cases it works,
in other cases it doesn't
as illustrated in following example.


<html>
<head>
<title>Functions</title>

<script type="text/javascript">
Greet("Fred");
</script>

<script type="text/javascript">
Greet("Ethel");

function Greet(who) {
alert("Greetings, " + who);
}
</script>

</head>
<body>
<h1>
Function Example</h1>
</body>
</html>

Greet("Ethel") works fine but Greet("Fred"); doesn't work.

Both invocations are placed BEFORE the function-definition but
apparently it is not the determining factor.
Is it because they are placed in different <script> tags? if so, why
then?

thank you
Chris
 
M

Martin Honnen

Chris said:
Greet("Ethel") works fine but Greet("Fred"); doesn't work.

Both invocations are placed BEFORE the function-definition but
apparently it is not the determining factor.
Is it because they are placed in different <script> tags? if so, why
then?

Well the script code in each script element (or in the file referenced
by the src attribute) is evaluated as an ECMAScript program. Such a
program is defined as a sequence of SourceElements where a SourceElement
can be a Statement or a FunctionDeclaration and the evaluation is
defined as processing first all FunctionDeclarations and then evaluating
the statements.
That way you can have a statement with a function call before the
function declaration, but only within the code contained in one script
element.
 
T

Thomas 'PointedEars' Lahn

Martin said:
Well the script code in each script element (or in the file referenced
by the src attribute) is evaluated as an ECMAScript program. Such a
program is defined as a sequence of SourceElements where a SourceElement
can be a Statement or a FunctionDeclaration and the evaluation is
defined as processing first all FunctionDeclarations and then evaluating
the statements.
That way you can have a statement with a function call before the
function declaration, but only within the code contained in one script
element.

This is what happens here, but there is no requirement for that. The
observed behavior is unexpected indeed, as variable instantiation is
specified to come before execution. Calling the function in a different,
later `script' element than in which it was declared works, which indicates
that separate `script' elements do not constitute separate Programs per se.


PointedEars
 
C

Chris

This is what happens here, but there is no requirement for that.  The
observed behavior is unexpected indeed, as variable instantiation is
specified to come before execution.  Calling the function in a different,
later `script' element than in which it was declared works, which indicates
that separate `script' elements do not constitute separate Programs per se.

PointedEars
--
    realism:    HTML 4.01 Strict
    evangelism: XHTML 1.0 Strict
    madness:    XHTML 1.1 as application/xhtml+xml
                                                    -- Bjoern Hoehrmann

thank you
Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top