Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > evaluating a object string

Reply
Thread Tools

evaluating a object string

 
 
jman
Guest
Posts: n/a
 
      03-06-2008
var x = eval("{ 'flag' : 1 }");
alert(x);
alert(x.flag);

why doesn't the above work?


 
Reply With Quote
 
 
 
 
Henry
Guest
Posts: n/a
 
      03-06-2008
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.
 
Reply With Quote
 
 
 
 
SAM
Guest
Posts: n/a
 
      03-06-2008
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
 
Reply With Quote
 
Henry
Guest
Posts: n/a
 
      03-06-2008
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 }"+")");
 
Reply With Quote
 
SAM
Guest
Posts: n/a
 
      03-06-2008
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.
 
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
Truth value evaluating of an object Byron Ruby 0 10-28-2007 01:26 AM
Truth value evaluating of an object Dave River Ruby 4 10-27-2007 11:30 PM
Java program is not evaluating String value correctly... Shelly Java 8 12-10-2004 04:12 PM
negative number evaluating greater than string.size() Jason C++ 25 02-22-2004 08:43 PM
newbie question on evaluating a string variable John Boik C Programming 2 08-16-2003 07:03 PM



Advertisments