Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Weird nested function behavior in IE

1 view
Skip to first unread message

Ryan Breen

unread,
Nov 2, 2004, 5:18:01 PM11/2/04
to
The following:

if(true)
{
outer.prototype.inner=hello_world;
function outer()
{
this.inner();
}

function hello_world()
{
alert("HI");
}

var hold_me =new outer();
}

works in IE but fails in Firefox or Rhino. In Rhino, the error is
that 'outer' is evaluated as a FUNCTION_EXPRESSION_STATEMENT rather
than a FUNCTION_STATEMENT but fn.itsClosure is null. Has anyone seen
anything like this before?

FYI, I'm using 1.5R3.

Igor Bukanov

unread,
Nov 2, 2004, 5:42:27 PM11/2/04
to Ryan Breen

Note that this code does not confirm to ECMAScript standard since
ECMAScript allows function statements only at the top level of script or
function.

Now MS JScript and SpiderMonkey/Rhino extends that allows to define
function statements anywhere were a JS statement is allowed, but the
semantics is different:

In JScript all such function statements are treated in the same way and
effectively evaluated at the start of script/function so you can refer
the functions by names right away.

In SM/Rhino such functions are defined only when the corresponding code
reaches the point of the "extended function statement" declaration which
allows to have a conditional compilation.

For example given:

if (condition) {
function test1() {
}
}

function test2() {
}


then JScript would always define test1 even if condition is false while
SM/Rhino would define it only when condition is true.

To make you example to work move hello_world()/outer to the beginning of
the if block.

Regards, Igor

Ryan Breen

unread,
Nov 2, 2004, 11:49:18 PM11/2/04
to ig...@fastmail.fm
On Tue, 02 Nov 2004 23:42:27 +0100, Igor Bukanov wrote:
> To make you example to work move hello_world()/outer to the beginning of
> the if block.

Igor,

Thanks for the feedback. Unfortunately, that's not an option in this case
-- I'm parsing existing, IE-specific JScript. Is my only option
deep-Rhino-hacking to treat all functions as FUNCTION_STATEMENTS rather
than FUNCTION_EXPRESSION_STATEMENTS?

Ryan

Igor Bukanov

unread,
Nov 3, 2004, 2:36:56 AM11/3/04
to Ryan Breen
Ryan Breen wrote:
> Unfortunately, that's not an option in this case
> -- I'm parsing existing, IE-specific JScript. Is my only option
> deep-Rhino-hacking to treat all functions as FUNCTION_STATEMENTS rather
> than FUNCTION_EXPRESSION_STATEMENTS?

Yes, that should do the trick.

Regards, Igor

0 new messages