473,739 Members | 2,602 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 FunctionDeclara tion ... create a property of the
variable object whose name is the Identifier... whose value is ... a
Function object"
but also
13: For FunctionDeclara tion: "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 1784
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 FunctionDeclara tion ... create a property of the
variable object whose name is the Identifier... whose value is ... a
Function object"
but also
13: For FunctionDeclara tion: "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 FunctionDeclara tion ... create a property of the
variable object whose name is the Identifier... whose value is ... a
Function object"
but also
13: For FunctionDeclara tion: "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 bugRiddenCrashP ronePieceOfJunk = (
navigator.userA gent.indexOf('M SIE 5') != -1
&& navigator.userA gent.indexOf('M ac') != -1
) // Plone, register_functi on.js:16
Sep 29 '07 #5

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

Similar topics

83
6510
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 C. It can cause very ugly errors,like this: epsilon=0 S=0 while epsilon<10: S=S+epsilon
7
3803
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. And why would one choose one over the other. moreover, I thought there was a difference between the two below where one would not let the value change whereas the other one would not let the
5
41035
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 on the previous definition of typedef, I can't understand the next:
42
5618
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
10756
by: spasmous | last post by:
main() { float * f; initialize_f(f); // ...use f for processing free(f); }
13
1704
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." here: http://jibbering.com/faq/faq_notes/closures.html
15
2955
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 linux system tells me that i have to include math.h so it seems like I'm doing the right thing in order to be able to use the lgamma function. When I compile the .c file with
20
2373
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
16454
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 subset of C/C++, thus will have the same syntax as C/C++. Somehow the interpreted code must be able to store generic function pointers because there is no way for the interpreter to know every possible function signature in advance. I was...
0
8969
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8792
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9266
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9209
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8215
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6754
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6054
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4570
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2748
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.