Question about eval scope

Discussion in 'Javascript' started by jdc_1040, Jan 16, 2006.

  1. jdc_1040

    jdc_1040 Guest

    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.
    jdc_1040, Jan 16, 2006
    #1
    1. Advertising

  2. jdc_1040

    VK Guest

    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>
    VK, Jan 16, 2006
    #2
    1. Advertising

  3. jdc_1040

    Randy Webb Guest

    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/
    Randy Webb, Jan 16, 2006
    #3
  4. jdc_1040

    jdc_1040 Guest

    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.
    jdc_1040, Jan 16, 2006
    #4
  5. jdc_1040

    jdc_1040 Guest

    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. :)
    jdc_1040, Jan 16, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Eric Newton
    Replies:
    3
    Views:
    9,353
    Brock Allen
    Apr 4, 2005
  2. DataBinder.Eval and Eval.

    , Jun 16, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    523
    Karl Seguin [MVP]
    Jun 16, 2006
  3. Alex van der Spek

    eval('07') works, eval('08') fails, why?

    Alex van der Spek, Jan 8, 2009, in forum: Python
    Replies:
    6
    Views:
    1,404
    Bruno Desthuilliers
    Jan 8, 2009
  4. Liang Wang
    Replies:
    8
    Views:
    124
    Ben Morrow
    Feb 2, 2008
  5. Marc Girod

    to eval or not to eval?

    Marc Girod, Apr 19, 2011, in forum: Perl Misc
    Replies:
    2
    Views:
    149
    Marc Girod
    Apr 19, 2011
Loading...

Share This Page