From: FAQ server on
-----------------------------------------------------------------------
FAQ Topic - When should I use eval?
-----------------------------------------------------------------------

The ` eval ` function should _only_ be used when it is necessary to
evaluate a string supplied or composed at run-time; the string
can be anything from a simple (but unpredictable) expression such
as ` "12 * 2.54" ` to a substantial piece of javascript code.

When ` eval( '{"key" : 42}' ) ` is called, ` { ` is interpreted as a block of
code instead of an object literal. Hence, the Grouping Operator (parentheses)
is used to force ` eval ` to interpret the JSON as an object literal:
` eval( '({"key" : 42})' ); `.

http://json.org/

http://jibbering.com/faq/#propertyAccessAgain

http://jibbering.com/faq/notes/square-brackets/


The complete comp.lang.javascript FAQ is at
http://jibbering.com/faq/

--

The sendings of these daily posts are proficiently hosted
by http://www.pair.com.

From: Dr J R Stockton on
In comp.lang.javascript message <4c0833fa$0$281$14726298(a)news.sunsite.dk
>, Thu, 3 Jun 2010 23:00:03, FAQ server <javascript(a)dotinternet.be>
posted:

>-----------------------------------------------------------------------
>FAQ Topic - When should I use eval?
>-----------------------------------------------------------------------
>
>The ` eval ` function should _only_ be used when it is necessary to
>evaluate a string supplied or composed at run-time; the string
>can be anything from a simple (but unpredictable) expression such
>as ` "12 * 2.54" ` to a substantial piece of javascript code.

Also, eval is useful if, cross-browser, a piece of code needs to be both
executable and shown in full (layout, comment, etc.).


>When ` eval( '{"key" : 42}' ) ` is called, ` { ` is interpreted as a block of
>code instead of an object literal.

You are saying there, explicitly, that a single character is interpreted
as a block of code. That is unreasonable.


--
(c) John Stockton, nr London UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
Proper <= 4-line sig. separator as above, a line exactly "-- " (RFCs 5536/7)
Do not Mail News to me. Before a reply, quote with ">" or "> " (RFCs 5536/7)
From: Evertjan. on
Dr J R Stockton wrote on 05 jun 2010 in comp.lang.javascript:

>>When ` eval( '{"key" : 42}' ) ` is called, ` { ` is interpreted as a
>>block of code instead of an object literal.
>
> You are saying there, explicitly, that a single character is
> interpreted as a block of code. That is unreasonable.

Unreasonable in the sense of 'not showing good judgment' or 'excessive'?

I suppose you mean 'incorrect', and that the sentence should be:

` { ... } ` is interpreted as a block of code instead of an object literal.

===============

Why cann't '{...}' be both a string containing a [block of] code,
and also be an 'object litteral'?

What is an 'object litteral' here anyway but a string containing a block of
code that defines an object?

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
From: Dr J R Stockton on
In comp.lang.javascript message <Xns9D8F68134F11Deejj99(a)194.109.133.242>
, Sun, 6 Jun 2010 08:13:51, Evertjan. <exjxw.hannivoort(a)interxnl.net>
posted:
>Dr J R Stockton wrote on 05 jun 2010 in comp.lang.javascript:
>
>>>When ` eval( '{"key" : 42}' ) ` is called, ` { ` is interpreted as a
>>>block of code instead of an object literal.
>>
>> You are saying there, explicitly, that a single character is
>> interpreted as a block of code. That is unreasonable.
>
>Unreasonable in the sense of 'not showing good judgment' or 'excessive'?
>
>I suppose you mean 'incorrect',

More accurately, "daft" ("gek"?).


>Why cann't '{...}' be both a string containing a [block of] code,
>and also be an 'object litteral'?

The context of that sentence implies firmly that it is a JavaScript
string, and implies nothing more.

>What is an 'object litteral' here anyway but a string containing a block of
>code that defines an object?

A string S cannot be an Object Literal. While eval(S) may give an
Object (e.g. if S = "A = {} ; A.b = 9 ; A"), neither it nor S are
themselves Object Literals.

When eval( '{"key" : 42}' ) is called, Firefox 3.0.19 says "Error:
invalid label" and points to the position between " and k. It does not
reach the "}". Therefore, no block of code is found or interpreted.
But it appears that the "{" is considered as the start of a block of
code. Interpretation clearly reaches the ":" (as it sees a "label"
situation) and then finds the first '"' to be impermissible.

So it should have "When ` eval( '{"key" : 42}' ) ` is called, ` { ` is
interpreted as starting a block of code rather than as starting an
object literal."

Except in circumstances which do not apply here (such as function
definitions and calls, and "for" statements), and AFAIR, the content of
a pair of parentheses must always be an expression or thereabouts. So
it might be better to then put "Parentheses will force ` eval ` to
interpret ` {"key" : 42} ` as an expression consisting of an object
literal: use ` eval( '({"key" : 42})' ); `."

The term "JSON" should not be used in this section; the reader may know
nothing of JSON, and those who know JSON will recognise the possibility
that the string is intended to be JSON.

--
(c) John Stockton, nr London UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
Proper <= 4-line sig. separator as above, a line exactly "-- " (RFCs 5536/7)
Do not Mail News to me. Before a reply, quote with ">" or "> " (RFCs 5536/7)
From: Evertjan. on
Dr J R Stockton wrote on 07 jun 2010 in comp.lang.javascript:

> In comp.lang.javascript message
> <Xns9D8F68134F11Deejj99(a)194.109.133.242> , Sun, 6 Jun 2010 08:13:51,
> Evertjan. <exjxw.hannivoort(a)interxnl.net> posted:
>>Dr J R Stockton wrote on 05 jun 2010 in comp.lang.javascript:
>>
>>>>When ` eval( '{"key" : 42}' ) ` is called, ` { ` is interpreted as a
>>>>block of code instead of an object literal.
>>>
>>> You are saying there, explicitly, that a single character is
>>> interpreted as a block of code. That is unreasonable.
>>
>>Unreasonable in the sense of 'not showing good judgment' or
>>'excessive'?
>>
>>I suppose you mean 'incorrect',
>
> More accurately, "daft" ("gek"?).

Witch is not the same as incorrect,
as 'daft' things ccould be correct,
or even both correct and incorrect at the same time
like in the realm of the relativistic quantum mechanics.

;-)

>>Why cann't '{...}' be both a string containing a [block of] code,
>>and also be an 'object litteral'?
>
> The context of that sentence implies firmly that it is a JavaScript
> string, and implies nothing more.
>
>>What is an 'object litteral' here anyway but a string containing a
>>block of code that defines an object?
>
> A string S cannot be an Object Literal. While eval(S) may give an
> Object (e.g. if S = "A = {} ; A.b = 9 ; A"), neither it nor S are
> themselves Object Literals.
>
> When eval( '{"key" : 42}' ) is called, Firefox 3.0.19 says "Error:
> invalid label" and points to the position between " and k. It does
> not reach the "}". Therefore, no block of code is found or
> interpreted. But it appears that the "{" is considered as the start of
> a block of code. Interpretation clearly reaches the ":" (as it sees a
> "label" situation) and then finds the first '"' to be impermissible.
>
> So it should have "When ` eval( '{"key" : 42}' ) ` is called, ` { `
> is interpreted as starting a block of code rather than as starting an
> object literal."
>
> Except in circumstances which do not apply here (such as function
> definitions and calls, and "for" statements), and AFAIR, the content
> of a pair of parentheses must always be an expression or thereabouts.
> So it might be better to then put "Parentheses will force ` eval `
> to interpret ` {"key" : 42} ` as an expression consisting of an object
> literal: use ` eval( '({"key" : 42})' ); `."

In essence I still have no idea what an 'object-litteral' is.

It must be a string as part of a script-code, meseems, that is to be
converted into an object-variable with or without content?

Like a 'string litteral' is a string as part of a script-code, meseems,
that is to be converted to a string-variable that is empty or has content?

> The term "JSON" should not be used in this section; the reader may
> know nothing of JSON, and those who know JSON will recognise the
> possibility that the string is intended to be JSON.

I agree.

I did not use it, but in't that just the name of a character-string that is
to be streamed to the client with Ajax, and then used as code to be
evaluated as the content of an object-variable, and has some further
restrictions on the key-type than strictly required?

These restrictions make it not 'just' an[y] 'object-litteral'.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)