Re: Little Help with JavaScript



On Oct 19, 5:16 pm, VK wrote:
VK wrote:
ashttp://json.orgstatesbecause its object data
structures are not JavaScript syntax compatible
Richard Cornford wrote:

Why is it that cannot even manage to do the simplest things correctly?
The attributions belong at the top, and the number of chevrons that
precede them should be precisely one less than the number of chevrons
that mark the quotes that each attribution refers to.

Yes they are.

Now you are definitely kidding.

No I am not. There are plenty of constructs that "syntax
compatible" (as you put it) that cannot just be dropped into source
code at whim with any expectation that the outcome will not be a
syntax error. For example, we have seen that - key1: - is fine for
labelling a statement, and to declare a property name in an object
literal but put it in an arguments list and you have a syntax error.
However, - key1: - is still a subset of javascript syntax regardless
of that.

{"key1":"value1"}
structure is a valid JSON object data but it is not JavaScript
syntax compatible for the above spelled reasons

It is fine as javascript source code, it is just the source code for
an Expression not a Statement.

and leads to "invalid label"
syntax error.

Only if you do not use it in a context appropriate for an Expression.

By creating the assignment context like
var obj = {"key1":"value1"}
we are masking this potential error

There is no potential error, particularly as asserting an object
literal is as worthless as writing:-

true;

and successfully instantiating obj
over assignment. If - as many others - we are using JavaScript
allowed shortcut without quotes around the key name then we also
masking this ticking bomb:
{"key1":"value1"}

An expression statement taking the form:-

({ key1 : "value"});

- is utterly worthless as the resulting object literal is then
inaccessible. If you are already writing worthless code then having:-

{ key1 : "value"}

- interpreted as a block statement containing a labelled statement is
not going to make anything any worse.

And
var obj = eval( '{key1:"value1"}' );
window.alert(typeof obj); // string
?

And
var obj = eval( '{"key1":"value1"}' );
// syntax error "invalid label"
?

And
var obj = eval('{"key1":"value1", "key2" : "value2"}');
// syntax error "invalid label"
?

But the - eval - function evaluates its string arguments as javascript
Programs, and the two structures that can make up a Program as
Statements and FunctionDeclarations. Your string arguments are clearly
not FunctionDeclarations so it should not be surprising if they get
interpreted as a Statement. And as has already been seen, an initial
opening curly brace in a Statement context can only be the beginning
of a Block statement. Whatever follows the brace can then only be
acceptable content for a Block statement or a syntax error, either way
it is never going to be an object literal as the only form of
statement that can consist of an Expression is an ExpressionStatement
and they are forbidden from starting with an opening brace.

Of course the 'solution' to the eval-ing JSON strings issues is a well
known as it is obvious (at least to anyone who understands the
language). You explicitly parenthesise the JSON expression and then
the result can be an ExpressionStatement because it no longer starts
with an opening brace (and that is never ambiguous with a Block as
statements are not allowed (directly, as they may still appear in the
bodies of contained function expressions) inside parenthesise). So:-

var obj = eval( '(' + JSON_String + ')' );

And so on? I did over last hours more than planned for the
week - grace to you and thank you again.

It remains pretty hard to believe that anyone is fool enough to employ
you to write computer software. The amount of time you must waste as a
result of taking the things in your head seriously is unimaginable.

Richard.
.


Quantcast