![]() | |
#11
| |||
| |||
|
|
On Sat, 24 Oct 2009 06:56:27 -0700 (PDT), VK wrote: ... By taking default key into quotes such reading goes away: var test = { op1 : false, op2 : false, 'default': false } window.alert(test.default); // false I honestly don't give a damn what ECMA says on it, it sucks even carved somewhere on a stone. With this and the preceding label vs key issue the safe coding policy will be changed: it will be required in the office and from subcontractors to use explicit quoting of object key values and do not ever relay on the implicit one. The group readers may or may not account this coding advise. If my memory serves me well, this is an old hat among several similar ones. Douglas Crockford, for example, has been preaching about this. He essentially says that JavaScript is a language with a number of well-known flaws, and he recommends to work around them conscientiously by using defensive strategies, usually a particular coding style and the avoidance of poor language elements. In this particular case I believe he recommends always to use quotes around property names in object literals, right? |
|
If I'm not mistaken, he has also made this a part of the JSON standard. |
|
Anyway, it may be worth reading his recommendations and perhaps use JSLint, which would have alerted you to the problem well in time. |
#12
| |||
| |||
|
|
The writer of Jason knew this already, [that keys should but do not always condone reserved words] it was the reason that he required all keys to be quoted. But the need in JSON follows from its use with unknowable data sources where the character sequences that will end up in the keys are unpredictable. For a programmer writing an object literal the character sequence that will be the key is known at the point of writing it, and |
|
so knowing the syntax rules for the object literal construct allows them to either see when the names they want to use need special handling or to question their choice of names. Blanket rules are not necessary in that context, and their application risks the "programmers" being drawn into a world of chanting sequences of mystical incantations in place of understanding what they are doing. It is interesting to note that JSON not only requires that all the keys be quoted, but that they be quoted with the double quote character (as opposed to the apostrophe). It is easy to see how such simplifications of format ease the string-based processing of the data. |
#13
| |||
| |||
|
|
I honestly don't give a damn what ECMA says on it, it sucks even carved somewhere on a stone. snip |
#14
| |||
| |||
|
|
Or just learning the syntax rules. VK claims to have been writing javascript for well in excess of 10 years, so his not already being familiar with object literal syntax rules is a little incredible (at least if the initial claim was regarded as credible). |

#15
| |||
| |||
|
|
Richard Cornford wrote on 24 okt 2009 in comp.lang.javascript: The writer of Jason knew this already, [that keys should but do not always condone reserved words] it was the reason that he required all keys to be quoted. But the need in JSON follows from its use with unknowable data sources where the character sequences that will end up in the keys are unpredictable. For a programmer writing an object literal the character sequence that will be the key is known at the point of writing it, and True, but the reserved word list is crossbrowserly perhaps knowable, but illogical. |
|
so knowing the syntax rules for the object literal construct allows them to either see when the names they want to use need special handling or to question their choice of names. Blanket rules are not necessary in that context, and their application risks the "programmers" being drawn into a world of chanting sequences of mystical incantations in place of understanding what they are doing. It is interesting to note that JSON not only requires that all the keys be quoted, but that they be quoted with the double quote character (as opposed to the apostrophe). It is easy to see how such simplifications of format ease the string-based processing of the data. Perhaps ours is not to reason why [cf Alfred Lord Tennyson], |
|
but I wrote it to show the reasoning, not as a general advice to be followed up. A parallel is the [ugly?] habit to [] fieldnames in sql strings, also to preclude possible reserved word errors. |
#16
| |||
| |||
|
|
The man who invented the language said it must be like that. Get used to it. If you can't, find another language. |
#17
| |||
| |||
|
|
John G Harris wrote: The man who invented the language said it must be like that. Get used to it. If you can't, find another language. I wouldn't put the strict equality sign between what the Man did and what others (ECMA freelancers) managed to write about it. |
#18
| |||
| |||
|
|
Thomas 'PointedEars' Lahn really killed me recently after years of quoting to death different papers and then not knowing that delete this.val is not allowed at least in IE. |
|
For me it's like if someone knows by heart all von Clausewitz and all field regulations yet not able to reload his own rifle ![]() |
#19
| |||
| |||
|
|
I honestly don't give a damn what ECMA says on it, it sucks even carved somewhere on a stone. With this and the preceding label vs key issue the safe coding policy will be changed: it will be required in the office and from subcontractors to use explicit quoting of object key values and do not ever relay on the implicit one. The group readers may or may not account this coding advise. |
#20
| |||
| |||
|
|
For the less perceptive (like you): "default" is a reserved word in JS. |
![]() |
| Thread Tools | |
| Display Modes | |
| |