variable declaration inside eval inside with

  • Thread starter Mehmet Yavuz S. Soyturk
  • Start date
M

Mehmet Yavuz S. Soyturk

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.
 
R

Richard Cornford

Mehmet Yavuz S. Soyturk wrote:
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.

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.

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.
 
M

Mehmet Yavuz S. Soyturk

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).
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top