Connecting Tech Pros Worldwide Help | Site Map

evaluating a object string

 
LinkBack Thread Tools Search this Thread
  #1  
Old March 6th, 2008, 11:25 AM
jman
Guest
 
Posts: n/a
Default evaluating a object string

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

why doesn't the above work?



  #2  
Old March 6th, 2008, 11:45 AM
Henry
Guest
 
Posts: n/a
Default Re: evaluating a object string

On Mar 6, 12:17*pm, jman wrote:
Quote:
* * * * * * 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.
  #3  
Old March 6th, 2008, 12:15 PM
SAM
Guest
 
Posts: n/a
Default Re: evaluating a object string

Henry a écrit :
Quote:
On Mar 6, 12:17 pm, jman wrote:
Quote:
> 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
  #4  
Old March 6th, 2008, 12:25 PM
Henry
Guest
 
Posts: n/a
Default Re: evaluating a object string

On Mar 6, 1:07*pm, SAM wrote:
<snip>
Quote:
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 }"+")");
  #5  
Old March 6th, 2008, 06:55 PM
SAM
Guest
 
Posts: n/a
Default Re: evaluating a object string

Henry a écrit :
Quote:
On Mar 6, 1:07 pm, SAM wrote:
<snip>
Quote:
>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.
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.