473,396 Members | 1,734 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,396 software developers and data experts.

are inner functions always properties?

Hi,
given two nested functions:

function outer() {
function inner() {
}
}

Is it determined by the ECMAScript standard that inner should be a
property of outer, thus a "document.write(outer.inner);" on the global
level outputs something like "function inner() { }"?
Olov Johansson

Jan 17 '06 #1
8 1505


ol************@gmail.com wrote:

given two nested functions:

function outer() {
function inner() {
}
}

Is it determined by the ECMAScript standard that inner should be a
property of outer,


No, but Spidermonkey, the JavaScript engine in Netscape and Mozilla did
that for quite some time (for instance in Netscape 4.7, still in Mozilla
1.7).
But no longer in Firefox 1.5.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Jan 17 '06 #2
On 17/01/2006 17:20, ol************@gmail.com wrote:
function outer() {
function inner() {
}
}

Is it determined by the ECMAScript standard that inner should be a
property of outer [...]


No. Inner functions are local. It is almost exactly the same as:

function outer() {
var inner = function() {};
}

The only reason why an inner function should be a property of an outer
function is if it was assigned such during execution of the function body:

function outer() {
function inner() {}

outer.inner = inner;

/* Or:
* arguments.callee.inner = inner;
*/
}

Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Jan 17 '06 #3
VK

ol************@gmail.com wrote:
Hi,
given two nested functions:

function outer() {
function inner() {
}
}

Is it determined by the ECMAScript standard that inner should be a
property of outer, thus a "document.write(outer.inner);" on the global
level outputs something like "function inner() { }"?


Unless you have killed toString() method for Function like:
....
Function.constructor.prototype.toString = function(){return
'function';}
....
Then you cannot have runtime access anymore to function bodies.

Jan 17 '06 #4
Thanks for your answers,
almost exactly you say, then what is the resulting difference
(according to the standard) for our two simple examples?

function outer() {
function inner() {
}
}

function outer() {
var inner = function() {
};
}
Olov Johansson

Jan 19 '06 #5
VK

Olov Johansson wrote:
Thanks for your answers,
almost exactly you say, then what is the resulting difference
(according to the standard) for our two simple examples?

function outer() {
function inner() {
}
}

function outer() {
var inner = function() {
};
}


If "according to the standard" then the first variant is simply illegal
as it is not allowed to nest function declarations. Whatever results
will be on a prticular browser - it depends on that particular browser.

The second variant creates local variable inner with the reference on
an anonymous function.

Jan 19 '06 #6
Nested functions (with lexical scope and closures) is indeed in the
standard. Have a look at
http://www.ecma-international.org/pu...t/ECMA-262.pdf
..
Olov Johansson

Jan 19 '06 #7
Olov Johansson wrote:
Thanks for your answers,
almost exactly you say, then what is the resulting
difference (according to the standard) for our two
simple examples?

function outer() {
function inner() {
}
}

function outer() {
var inner = function() {
};
}


The only difference is when the inner function object(s) is(are)
created. With a function declaration the function object is created and
assigned to a named property of the Activation/variable object during
'variable instantiation', prior to the execution of any code in the
function body. The second example creates a named property of the
Activation/variable object during variable instantiation but the
function object is not created until the right hand side of the
assignment expression is evaluated, during the execution of the function
body.

Richard.
Jan 19 '06 #8
On 19/01/2006 09:14, Olov Johansson wrote:
Nested functions (with lexical scope and closures) is indeed in the
standard.


Ignore VK.

As Richard said, the difference lies in when the function objects would
be created. The scope chain and other behavioural features remain the same.

function outer() {
inner();

function inner() {
}
}

would work as expected because the nested function, inner, would have
been created before execution of the function body had started.

function outer() {
inner();

var inner = function() {};
}

would fail. The local variable, inner, would exist (variable
declarations, like function declarations, occur before execution) but
the function object would not have been created, and a reference
assigned, until execution had reached that point in the function body.

[snip]

Mike
When replying through Google Groups, please do not use the 'Reply' link
at the end of the post. Instead, activate 'show options' at the top of
the post you want to reply to, and use 'Reply' from there. That post
will now be quoted.

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Jan 19 '06 #9

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

Similar topics

2
by: Irlan agous | last post by:
Hello i have t tables reactie and form and this query $sql = "select reactie.persid,form.oproep,form.foto,form.id from reactie,form INNER JOIN reactie ON (reactie.persid = form.id) group by...
7
by: Wolfgang Jeltsch | last post by:
Hello, I want to write a list class with an iterator class as an inner class. The iterator class must have access to certain private members of the list class in order to do its job. Here is a...
1
by: optimistx | last post by:
How to build or find an object browser for javascript? E.g. Delphi integrated developement environment ('ide') offers a very practical object browser for Pascal language. When typing a name with...
3
by: cybertof | last post by:
Hello, Is it possible in C# to have 2 overloaded functions with - same names - same parameters - different return type values If no, is it possible in another language ?
11
by: Kondapanaidu | last post by:
Hi, What is the difference between Properties and functions. Why dont we go for functions instead of properties. Regrads
4
by: trevor_morgan | last post by:
The following code: def functions(): l=list() for i in range(5): def inner(): return i l.append(inner) return l
3
by: Dan Jacobson | last post by:
Let's say all one can do to override a document's link colors, <a href="FAQ.aspx"><font color="#0000ff">FAQ</font></a> is inject things like: <a href="FAQ.aspx"><span style="color:green"><font...
7
by: chromis | last post by:
Hi there, I have three tables: -properties -propLocations -locations I am trying to allow each property to have more than one location. Locations holds the locations (id,name) and...
3
by: from.future.import | last post by:
Hi, I encountered garbage collection behaviour that I didn't expect when using a recursive function inside another function: the definition of the inner function seems to contain a circular...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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...
0
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,...

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.