Connecting Tech Pros Worldwide Forums | Help | Site Map

are inner functions always properties?

olov.johansson@gmail.com
Guest
 
Posts: n/a
#1: Jan 17 '06
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


Martin Honnen
Guest
 
Posts: n/a
#2: Jan 17 '06

re: are inner functions always properties?




olov.johansson@gmail.com wrote:

[color=blue]
> given two nested functions:
>
> function outer() {
> function inner() {
> }
> }
>
> Is it determined by the ECMAScript standard that inner should be a
> property of outer,[/color]

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/
Michael Winter
Guest
 
Posts: n/a
#3: Jan 17 '06

re: are inner functions always properties?


On 17/01/2006 17:20, olov.johansson@gmail.com wrote:
[color=blue]
> function outer() {
> function inner() {
> }
> }
>
> Is it determined by the ECMAScript standard that inner should be a
> property of outer [...][/color]

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.
VK
Guest
 
Posts: n/a
#4: Jan 17 '06

re: are inner functions always properties?



olov.johansson@gmail.com wrote:[color=blue]
> 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() { }"?[/color]

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.

Olov Johansson
Guest
 
Posts: n/a
#5: Jan 19 '06

re: are inner functions always properties?


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

VK
Guest
 
Posts: n/a
#6: Jan 19 '06

re: are inner functions always properties?



Olov Johansson wrote:[color=blue]
> 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() {
> };
> }[/color]

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.

Olov Johansson
Guest
 
Posts: n/a
#7: Jan 19 '06

re: are inner functions always properties?


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

Richard Cornford
Guest
 
Posts: n/a
#8: Jan 19 '06

re: are inner functions always properties?


Olov Johansson wrote:[color=blue]
> 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() {
> };
> }[/color]

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.


Michael Winter
Guest
 
Posts: n/a
#9: Jan 19 '06

re: are inner functions always properties?


On 19/01/2006 09:14, Olov Johansson wrote:
[color=blue]
> Nested functions (with lexical scope and closures) is indeed in the
> standard.[/color]

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.
Closed Thread