eval(string) scope ?

Discussion in 'Javascript' started by Jocke P, Jan 8, 2010.

  1. Jocke P

    Jocke P Guest

    Hi,

    I had the idea to create a walkthrough sessions for users,
    printing statements, then executing one by one.

    I find that eval on separate strings doesn't put values in the same scope,
    so any statement using an earlier value throws ReferenceError.

    Here's a minimal session:

    session = [
    "/* Hit enter to execute next statement.\nType 'quit' to stop session. */",
    "x = 'test'",
    "if (x == 'test') alert('wohoo!')"
    ]

    Here's code to execute it. It uses a custom method in my V8 shell:

    for(line in session) {
    input = prompt(session[line]); // prompt wraps C getline()
    if (input == 'quit') break;
    eval(session[line]);
    }

    Apparently eval operates on each line separately,
    so I get reference error on the last line in session.

    How could I connect the evals so they operate on the same scope?

    Thanks,

    Jocke
     
    Jocke P, Jan 8, 2010
    #1
    1. Advertising

  2. Jocke P

    Jocke P Guest

    In the asking, a google session suggests itself, providing the answer.
    Pack it all in a function and I'm roaling.

    function RunSession() {
    session = [ ...]
    for(line in session) eval(session[line]) ...etc
    }

    All values end up in the global object as well, so usable after the session. Nice.

    Oh well, cheers 2 all anyway,

    jp

    Jocke P wrote:
    > Hi,
    >
    > I had the idea to create a walkthrough sessions for users,
    > printing statements, then executing one by one.
    >
    > I find that eval on separate strings doesn't put values in the same scope,
    > so any statement using an earlier value throws ReferenceError.
    >
     
    Jocke P, Jan 9, 2010
    #2
    1. Advertising

  3. Jocke P <> writes:

    > I had the idea to create a walkthrough sessions for users,
    > printing statements, then executing one by one.
    >
    > I find that eval on separate strings doesn't put values in the same scope,
    > so any statement using an earlier value throws ReferenceError.


    They should, so you are doing something wrong.
    The scope of a direct call to eval is the surrounding scope. Variables
    are created in that scope. The following should work:

    var lines = ["var x = 42;", "var y = x + x;", "alert(y);"];
    for (var i = 0; i < lines.length; i++) { eval(lines); }

    > Here's a minimal session:
    >
    > session = [
    > "/* Hit enter to execute next statement.\nType 'quit' to stop session. */",
    > "x = 'test'",


    Here you don't declare x as a variable at all, so it should be created
    as a property of the global object.

    > "if (x == 'test') alert('wohoo!')"
    > ]
    >
    > Here's code to execute it. It uses a custom method in my V8 shell:
    >
    > for(line in session) {
    > input = prompt(session[line]); // prompt wraps C getline()
    > if (input == 'quit') break;
    > eval(session[line]);
    > }


    Running your test in Chrome gives the "wohoo!" alert.

    > Apparently eval operates on each line separately,


    Yes.

    > so I get reference error on the last line in session.


    No. At least not for that reason.

    > How could I connect the evals so they operate on the same scope?


    They should do. You need to tell us more about what you do.

    /L
    --
    Lasse Reichstein Holst Nielsen
    'Javascript frameworks is a disruptive technology'
     
    Lasse Reichstein Nielsen, Jan 9, 2010
    #3
  4. Jocke P

    JR Guest

    On Jan 8, 9:40 pm, Jocke P <> wrote:
    > Hi,
    >
    > I had the idea to create a walkthrough sessions for users,
    > printing statements, then executing one by one.
    >
    > I find that eval on separate strings doesn't put values in the same scope,
    > so any statement using an earlier value throws ReferenceError.
    >
    > Here's a minimal session:
    >
    > session = [
    >    "/* Hit enter to execute next statement.\nType 'quit' to stop session. */",
    >    "x = 'test'",
    >    "if (x == 'test') alert('wohoo!')"
    > ]
    >
    > Here's code to execute it. It uses a custom method in my V8 shell:
    >
    > for(line in session) {
    >    input = prompt(session[line]);  // prompt wraps C getline()
    >    if (input == 'quit') break;
    >    eval(session[line]);
    >
    > }
    >
    > Apparently eval operates on each line separately,
    > so I get reference error on the last line in session.
    >
    > How could I connect the evals so they operate on the same scope?
    >
    > Thanks,
    >
    >         Jocke


    The for-in statement is an object iterator. Quoting Douglas Crockford:
    "- Do not use Array when you do not need .length. You should be using
    an Object. It is best to not use the term "associative array" when
    working in JavaScript because it will confuse you."

    According to Ecma-262, section 12.6.4, "The mechanics of enumerating
    the properties [...] is implementation dependent." The order of
    enumeration is defined by the object; in other words: the order in
    which the *lines* (in session) are produced is not guaranteed.

    --
    JR
     
    JR, Jan 9, 2010
    #4
    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,621
    Brock Allen
    Apr 4, 2005
  2. DataBinder.Eval and Eval.

    , Jun 16, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    583
    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,561
    Bruno Desthuilliers
    Jan 8, 2009
  4. Liang Wang
    Replies:
    8
    Views:
    158
    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:
    177
    Marc Girod
    Apr 19, 2011
Loading...

Share This Page