are inner functions always properties?

O

olov.johansson

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
 
M

Martin Honnen

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.
 
M

Michael Winter

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
 
V

VK

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.
 
O

Olov Johansson

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
 
V

VK

Olov said:
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.
 
R

Richard Cornford

Olov said:
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.
 
M

Michael Winter

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top