variable declaration inside eval inside with

Discussion in 'Javascript' started by Mehmet Yavuz S. Soyturk, Apr 11, 2007.

  1. 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.
     
    Mehmet Yavuz S. Soyturk, Apr 11, 2007
    #1
    1. Advertising

  2. 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.
     
    Richard Cornford, Apr 12, 2007
    #2
    1. Advertising

  3. 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).
     
    Mehmet Yavuz S. Soyturk, Apr 12, 2007
    #3
  4. > Thanks, not it's more clear.

    Thanks, now it's more clear.
     
    Mehmet Yavuz S. Soyturk, Apr 12, 2007
    #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,547
    Brock Allen
    Apr 4, 2005
  2. DataBinder.Eval and Eval.

    , Jun 16, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    572
    Karl Seguin [MVP]
    Jun 16, 2006
  3. Bolin
    Replies:
    4
    Views:
    419
  4. 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,521
    Bruno Desthuilliers
    Jan 8, 2009
  5. Liang Wang
    Replies:
    8
    Views:
    147
    Ben Morrow
    Feb 2, 2008
Loading...

Share This Page