Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > eval(string) scope ?

Reply
Thread Tools

eval(string) scope ?

 
 
Jocke P
Guest
Posts: n/a
 
      01-08-2010
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
 
Reply With Quote
 
 
 
 
Jocke P
Guest
Posts: n/a
 
      01-09-2010
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.
>

 
Reply With Quote
 
 
 
 
Lasse Reichstein Nielsen
Guest
Posts: n/a
 
      01-09-2010
Jocke P <(E-Mail Removed)> 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[i]); }

> 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'

 
Reply With Quote
 
JR
Guest
Posts: n/a
 
      01-09-2010
On Jan 8, 9:40*pm, Jocke P <(E-Mail Removed)> 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
 
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
Re: Lexical scope vs. dynamic scope Xah Lee Java 0 02-26-2009 10:08 AM
CSPEC issue: lossing scope (or incorrect scope) in cspec subroutine. balldarrens@gmail.com Perl Misc 0 02-05-2009 08:42 PM
Scope - do I need two identical classes, each with different scope? ann Java 13 09-13-2005 03:07 AM
How do namespace scope and class scope differ? Steven T. Hatton C++ 9 07-19-2005 06:07 PM
IMPORT STATIC; Why is "import static" file scope? Why not class scope? Paul Opal Java 12 10-10-2004 11:01 PM



Advertisments