On Thu, 2005-09-01 at 20:11 +0200, Thorsten Scherler wrote:
> Hi all,
> 
> I have a problem with the evalFunc() in flow. I think that not only the
> eval() function is broken, but as well the evalFunc().
> 
> Add the following to your flow script:
> 
> var genericDoc;
> scriptString= "genericDoc='testing';";
> evalFunc = new Function ("genericDoc",scriptString);
> evalFunc(genericDoc);
> print("xxx ",genericDoc);
> 
> The sysout gives you:
> xxx undefined
> 
> That is not what the following link says (thx again Jason Johnston, for
> this awesome link):
> http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Objects:Function#Created_By
> "However, if you pass an object as a parameter to a function and the
> function changes the object's properties, that change is visible outside
> the function."

Your code is actually not "changing the object's properties".  If you
were to try genericDoc.property='value' then that would work since
you're changing a property of the object referred to by the genericDoc
variable.  However your code is actually changing which object
genericDoc refers to, which is not the same thing.  Remember, variables
are not objects but *pointers to* objects.

Now, you might expect that changing genericDoc's value inside the
function would also change the value of the genericDoc variable from
outside the function.  But this is not the case in your example because
function arguments are treated as variables local to the function, so
the genericDoc variable (the named argument) inside the function is
different than the genericDoc variable outside the function.

If you want genericDoc within the function to be the same variable as
genericDoc outside, you can rely on closures.  This basically means that
when a function is created (as happens with new Function("")) it creates
a closure so that the code within the function has access to the entire
scope of where it was created.  So:

var genericDoc;
scriptString= "genericDoc='testing';";
evalFunc = new Function (scriptString);
evalFunc();
print("xxx ",genericDoc);

...should work, since evalFunc automatically gets access to the
genericDoc variable since it was in scope when evalFunc was created.

Hope this makes at least a little sense.  It's taken me a long while to
get a handle on the complexity of closures.

Reply via email to