![]() |
Question about eval scope
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. |
Re: Question about eval scope
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> |
Re: Question about eval scope
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/ |
Re: Question about eval scope
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. |
Re: Question about eval scope
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. :) |
| All times are GMT. The time now is 06:07 AM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.