473,326 Members | 2,196 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

function declaration semantics.

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

Sep 29 '07 #1
4 1766
fl*************@gmail.com wrote:
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
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann
Sep 29 '07 #2
fl*************@gmail.com wrote:
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.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Sep 29 '07 #3
On Sep 29, 3:13 pm, florian.loit...@gmail.com wrote:
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

Sep 29 '07 #4
fl*************@gmail.com wrote:
On Sep 29, 3:13 pm, florian.loit...@gmail.com wrote:
>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.)
Utter nonsense.
thanks to everybody who tried to help.
Thanks in advance for posting properly in the future.
PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
Sep 29 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

83
by: Alexander Zatvornitskiy | last post by:
Hello All! I'am novice in python, and I find one very bad thing (from my point of view) in language. There is no keyword or syntax to declare variable, like 'var' in Pascal, or special syntax in...
7
by: johny smith | last post by:
Can someone please explain to me the difference between these two: function1( const int a) function2( int const a) Both seemed to compile, but what is the difference between the two above....
5
by: Cancerbero | last post by:
Hi (first, excuse me for my bad english) As I know, the semantics for typedef is: typedef A B; I think this makes B a synonym of A, where A is an existing data type. Is that right? Based...
42
by: baumann | last post by:
hi all, typedef int (*pfunc)(int , int); pfunc a_func; i know it's ok, but how can define a_func without typedef statement? thanks .
20
by: spasmous | last post by:
main() { float * f; initialize_f(f); // ...use f for processing free(f); }
13
by: Jake Barnes | last post by:
I saw this sentence: "The last stage of variable instantiation is to create named properties of the Variable object that correspond with all the local variables declared within the function." ...
15
by: Bart Vandewoestyne | last post by:
I'm having a .c source file which at the top contains the line #include <math.h> In that source file, i declare a function dt which in its body uses the lgamma function. `man lgamma' on my...
20
by: Christian Christmann | last post by:
Hi, in a benchmark I've found an uncommon use of a function. This is the simplified form: 1 int foo( int f ) 2 { 3 return f; 4 } 5
28
by: Peter Olcott | last post by:
I want to make a generic interface between a scripting language and native code, the native code and the interpreter will both be written in C++. The interpreter will probably be implemented as a...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.