Question about eval scope

J

jdc_1040

Could someone explain what the scope chain looks like to code running
inside eval? (Please no "eval is the devil" essays :) ) Let's say I
have a string of JavaScript that looks like this:

var myVar1 = 1;
var myVar2 = 3;

window.someFunc = function() { myVar1++; alertFunc(); };

function anotherFunc() { myVar2++; alertFunc(); }

function alertFunc() { alert(myVar1 + ' ' + myVar2); }

If I run that through eval (from within another function), I can then
do this:

window.someFunc();

and it'll show 1, then 2, then 3, etc. as I call it multiple times. So
I know that myVar1 is hanging around in memory somewhere along with
alertFunc. I know it's not attached to window, since
window.anotherFunc() gives me a standard "undefined" error.

I've also tried doing the eval like this:

function myFunction() { window.someObj = eval(theString); }

I was told this would attach the stuff defined in theString to someObj,
but window.someObj.someFunc is still undefined.

So what I'm wondering is, where are the variables and functions I
didn't explicitly assign to window defined? I've looked at the very
excellent FAQ article regarding scope chains and closures, but it
didn't cover what happens inside eval. Anyone know? Thanks in advance
for any insight.
 
V

VK

jdc_1040 said:
var myVar1 = 1;

window.someFunc = function() { myVar1++; alertFunc(); };

function anotherFunc() { myVar2++; alertFunc(); }

function alertFunc() { alert(myVar1 + ' ' + myVar2); }

If I run that through eval (from within another function), I can then
do this:

window.someFunc();

and it'll show 1, then 2, then 3, etc. as I call it multiple times. So
I know that myVar1 is hanging around in memory somewhere along with
alertFunc.

What do you mean "hanging around"? :)
In the posted code iIt is a properly declared global variable so it is
not ""hanging" but allocated in the script engine program stack.

Irrelevant to "eval" and besides FAQ's you should learn two higher
level scopes implemented by modern IE: DOM Document scope and Frame
scope.

They are not documented (yet?) but are very useful to know:
<http://groups.google.com/group/comp...0e7b79fa9b7/67ff9bdcd3b8bb38#67ff9bdcd3b8bb38>

<http://groups.google.com/group/comp..._frm/thread/bcadfcbde01a0b77/565e6accfcd8cd1d>
 
R

Randy Webb

jdc_1040 said the following on 1/16/2006 4:46 PM:
Could someone explain what the scope chain looks like to code running
inside eval? (Please no "eval is the devil" essays :) ) Let's say I
have a string of JavaScript that looks like this:

eval is executed in the current scope. So, its scope has the same scope
as if you weren't using eval. Nothing special about eval in that regards
as it doesn't change the scope of what its executing.
 
J

jdc_1040

Ok, "hanging around" was a bad choice of words. :) I understand that
it is properly instantiated. I'm just wondering what object has
properties allowing me to reach them - if any. I'm not sure that the
DOM Document scope and Frame scope would have any bearing if they are
specific to IE as I'm seeing the same results in Firefox, but I'll
check out the links you gave and see if I get any insight from them.
Thanks for the reply.
 
J

jdc_1040

Thanks Randy. Now that I think about it, that makes total since given
the behavior I was seeing, I was probably just staring at it too long
to see what was going on. :)
 

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

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top