Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Question about eval scope

Reply
Thread Tools

Question about eval scope

 
 
jdc_1040
Guest
Posts: n/a
 
      01-16-2006
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.

 
Reply With Quote
 
 
 
 
VK
Guest
Posts: n/a
 
      01-16-2006

jdc_1040 wrote:
> 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.lang.javascript/browse_frm/thread/1d67f0e7b79fa9b7/67ff9bdcd3b8bb38#67ff9bdcd3b8bb38>

<http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/bcadfcbde01a0b77/565e6accfcd8cd1d>

 
Reply With Quote
 
 
 
 
Randy Webb
Guest
Posts: n/a
 
      01-16-2006
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.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
 
Reply With Quote
 
jdc_1040
Guest
Posts: n/a
 
      01-16-2006
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.

 
Reply With Quote
 
jdc_1040
Guest
Posts: n/a
 
      01-16-2006
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
to eval or not to eval? Marc Girod Perl Misc 2 04-19-2011 01:13 PM
eval('07') works, eval('08') fails, why? Alex van der Spek Python 6 01-08-2009 08:24 PM
Different behavior between eval "07" and eval "08" Liang Wang Perl Misc 8 02-02-2008 08:31 PM
DataBinder.Eval and Eval. craigkenisston@hotmail.com ASP .Net 1 06-16-2006 05:33 PM
DataBinder.Eval for an object's property property... like Eval(Container.DataItem,"Version.Major") Eric Newton ASP .Net 3 04-04-2005 10:11 PM



Advertisments