function declaration semantics.

F

florian.loitsch

I wondered what should be the result of the following code:
===
function f() {
x = false;
function x() {};
alert(x);
}
===
According to Ecmascript-spec we have the following rules:
10.1.3: "For each FunctionDeclaration ... create a property of the
variable object whose name is the Identifier... whose value is ... a
Function object"
but also
13: For FunctionDeclaration: "1. Create a new Function object... 2.
Create a property of the current variable object (...) with name
Identifier and value Result(1)."

It seems that according to these rules, there should be two creations
of function objects for each Function Declaration. One when the
function starts, and one, when we reach the line of the function-
declaration. And following this reasoning the program should alert
"function x() {}", and not 'false'.

Any comments?
// florian loitsch
 
T

Thomas 'PointedEars' Lahn

I wondered what should be the result of the following code:
===
function f() {
x = false;
function x() {};
alert(x);
}
===
According to Ecmascript-spec we have the following rules:
10.1.3: "For each FunctionDeclaration ... create a property of the
variable object whose name is the Identifier... whose value is ... a
Function object"
but also
13: For FunctionDeclaration: "1. Create a new Function object... 2.
Create a property of the current variable object (...) with name
Identifier and value Result(1)."

It seems that according to these rules, there should be two creations
of function objects for each Function Declaration. One when the
function starts, and one, when we reach the line of the function-
declaration. And following this reasoning the program should alert
"function x() {}", and not 'false'.

Wrong, because variable instantiation takes place before execution, "on
entering the execution context" (ES3, 10.1.3). Therefore, once execution
enters the local execution context of `f', the following happens:

1. `x' is instantiated as a reference to the Function object.
2. `x' is set to `false'. The Function object can be garbage-collected.
3. `x' is read as `false', type-converted to "false" by alert(),
and displayed so.


HTH

PointedEars
 
M

Martin Honnen

It seems that according to these rules, there should be two creations
of function objects for each Function Declaration. One when the
function starts, and one, when we reach the line of the function-
declaration. And following this reasoning the program should alert
"function x() {}", and not 'false'.

The function body is first processed for function declarations, then the
statements in the function body are executed.
 
F

florian.loitsch

I wondered what should be the result of the following code:
===
function f() {
x = false;
function x() {};
alert(x);}

===
According to Ecmascript-spec we have the following rules:
10.1.3: "For each FunctionDeclaration ... create a property of the
variable object whose name is the Identifier... whose value is ... a
Function object"
but also
13: For FunctionDeclaration: "1. Create a new Function object... 2.
Create a property of the current variable object (...) with name
Identifier and value Result(1)."

It seems that according to these rules, there should be two creations
of function objects for each Function Declaration. One when the
function starts, and one, when we reach the line of the function-
declaration. And following this reasoning the program should alert
"function x() {}", and not 'false'.

Just found the answer: the Function-declarations are skipped inside
the
program/function-body due to the rules of section 14. (in particular
the
very last line of that section.)
thanks to everybody who tried to help.

// florian
 
T

Thomas 'PointedEars' Lahn

Just found the answer: the Function-declarations are skipped inside
the
program/function-body due to the rules of section 14. (in particular
the
very last line of that section.)

Utter nonsense.
thanks to everybody who tried to help.

Thanks in advance for posting properly in the future.


PointedEars
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top