By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,116 Members | 1,319 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,116 IT Pros & Developers. It's quick & easy.

are inner functions always properties?

P: n/a
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
Share this Question
Share on Google+
8 Replies


P: n/a


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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.