Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > variable declaration inside eval inside with

Reply
Thread Tools

variable declaration inside eval inside with

 
 
Mehmet Yavuz S. Soyturk
Guest
Posts: n/a
 
      04-11-2007
Hello,

consider the next code:

var obj = {};
with(obj) {
var x = 10;
}
print(x);
print(obj.x);

It prints 10 and undefined. Here, one could expect that obj.x get the
value 10. But it's not the case, because variable declarations are
placed at the start of function code (or global code), so the
previous code is equivalent with:

var obj;
var x;
obj = {};
with(obj) {
x = 10;
}
print(x);
print(obj.x);

You can clearly see now that x is placed in the outer context. But
consider the next:

var obj = {};
with(obj) {
eval("var x = 10;");
}
print(x);
print(obj.x);

I was expecting that obj.x would get the value 10 here. But no, it
gives the same output as the previous code. I tested it with
spidermonkey, kjs and ie jscript. Looking at the ECMA spec, I could
not find anything that describes that behaviour.

>From ECMA 262, sections 10.2.2 and 15.1.2.1, I can say that eval

executes in the same context as the body of the with statement, which
has obj as the variable object. var x = 10 is parsed as a program, and
executed in that context.
A program "var x = 10" creates a variable x in the current variable
object (in that case obj), and assigns it the value 10.

But why does it not like that in spidermonkey, kjs and ie? Did I
misinterpret the spec, or are they not standard compliant?

I know that one does not want to write code like this. I ask this,
because I'm writing a compiler for ECMAScript.

 
Reply With Quote
 
 
 
 
Richard Cornford
Guest
Posts: n/a
 
      04-12-2007
Mehmet Yavuz S. Soyturk wrote:
<snip>
> var obj = {};
> with(obj) {
> var x = 10;
> }
> print(x);
> print(obj.x);
>
> It prints 10 and undefined. Here, one could expect
> that obj.x get the value 10.


You could expect that, but it would be better not to.

<snip>
> var obj = {};
> with(obj) {
> eval("var x = 10;");
> }
> print(x);
> print(obj.x);
>
> I was expecting that obj.x would get the value 10 here. But no,
> it gives the same output as the previous code. I tested it with
> spidermonkey, kjs and ie jscript. Looking at the ECMA spec,
> I could not find anything that describes that behaviour.
>
>>From ECMA 262, sections 10.2.2 and 15.1.2.1, I can say that

> eval executes in the same context as the body of the with
> statement, which has obj as the variable object. var x = 10
> is parsed as a program, and executed in that context.
> A program "var x = 10" creates a variable x in the current
> variable object (in that case obj), and assigns it the value
> 10.


Your - obj - does not become the Variable object just because you put it
at the top of the scope chain, it just becomes the object at the top of
the scope chain. The order of the objects on the scope chain influences
the resolution of Identifiers not the behaviour of variable
instantiation.

Section 10.2.2 explicitly states that the calling context's Variable
object is used as the Variable object in the eval call.

> But why does it not like that in spidermonkey, kjs and ie?
> Did I misinterpret the spec, or are they not standard compliant?


You misinterpret the spec.

> I know that one does not want to write code like this. I ask
> this, because I'm writing a compiler for ECMAScript.


Richard.

 
Reply With Quote
 
 
 
 
Mehmet Yavuz S. Soyturk
Guest
Posts: n/a
 
      04-12-2007
Strange, google did not send my previous message.

> Your - obj - does not become the Variable object just because you put it
> at the top of the scope chain, it just becomes the object at the top of
> the scope chain. The order of the objects on the scope chain influences
> the resolution of Identifiers not the behaviour of variable
> instantiation.
>
> Section 10.2.2 explicitly states that the calling context's Variable
> object is used as the Variable object in the eval call.


Thanks, not it's more clear. I didn't see the distinction between the
Variable object and the front of the scope chain before.

As far as I can see, they are only different things in those cases:

- in the scope of a with block
- in the scope of a catch block
- in the upper scope of a named function expression (that one does not
have much significance, because the function can't create a new
variable in the upper scope).

 
Reply With Quote
 
Mehmet Yavuz S. Soyturk
Guest
Posts: n/a
 
      04-12-2007
> Thanks, not it's more clear.

Thanks, now it's more clear.

 
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
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
Variable declaration taken as a function pointer declaration Bolin C++ 4 12-02-2005 05:28 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