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.
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
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
Yes, that should do the trick.
Regards, Igor