evaluating a object string

Discussion in 'Javascript' started by jman, Mar 6, 2008.

  1. jman

    jman Guest

    var x = eval("{ 'flag' : 1 }");
    alert(x);
    alert(x.flag);

    why doesn't the above work?
     
    jman, Mar 6, 2008
    #1
    1. Advertising

  2. jman

    Henry Guest

    On Mar 6, 12:17 pm, jman wrote:
    >             var x = eval("{ 'flag' : 1 }");
    >             alert(x);
    >             alert(x.flag);
    >
    > why doesn't the above work?


    Because the - eval - method treats its string input as a javascript
    program and a javascript program commencing with an opening brace is a
    program that starts with a block statement. That makes the content of
    the block statement a syntax error.

    If you want to have your string interpreted as an object literal you
    have to force it into a context were it must be interpreted as an
    expression. Putting parenthesis around it will achieve that end.
     
    Henry, Mar 6, 2008
    #2
    1. Advertising

  3. jman

    SAM Guest

    Henry a écrit :
    > On Mar 6, 12:17 pm, jman wrote:
    >> var x = eval("{ 'flag' : 1 }");
    >> alert(x);
    >> alert(x.flag);
    >>
    >> why doesn't the above work?

    >
    > Because the - eval - method treats its string input as a javascript
    > program and a javascript program commencing with an opening brace is a
    > program that starts with a block statement. That makes the content of
    > the block statement a syntax error.
    >
    > If you want to have your string interpreted as an object literal you
    > have to force it into a context were it must be interpreted as an
    > expression. Putting parenthesis around it will achieve that end.


    can you give the code ?
    I didn't understand where to put these parenthesis
     
    SAM, Mar 6, 2008
    #3
  4. jman

    Henry Guest

    On Mar 6, 1:07 pm, SAM wrote:
    <snip>
    > can you give the code ?
    > I didn't understand where to put these parenthesis


    var x = eval("({ 'flag' : 1 })");

    - or:-

    var x = eval("("+"{ 'flag' : 1 }"+")");
     
    Henry, Mar 6, 2008
    #4
  5. jman

    SAM Guest

    Henry a écrit :
    > On Mar 6, 1:07 pm, SAM wrote:
    > <snip>
    >> can you give the code ?
    >> I didn't understand where to put these parenthesis

    >
    > var x = eval("({ 'flag' : 1 })");


    Thought having tried that ... ? !

    Thanks
    now it rests to understand how that works :)

    Perhaps there is a way to see step to step how 'eval' treats the string.
     
    SAM, Mar 6, 2008
    #5
    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. Shelly
    Replies:
    8
    Views:
    588
    Michael Borgwardt
    Dec 10, 2004
  2. Jason
    Replies:
    25
    Views:
    1,005
    Jorge Rivera
    Feb 22, 2004
  3. John Boik
    Replies:
    2
    Views:
    312
    Malcolm
    Aug 16, 2003
  4. Dave River
    Replies:
    4
    Views:
    149
    Morton Goldberg
    Oct 28, 2007
  5. Byron
    Replies:
    0
    Views:
    107
    Byron
    Oct 28, 2007
Loading...

Share This Page