Is this Javascript object literal key restriction strictly due to parsing?
Posted
by George Jempty
on Stack Overflow
See other posts from Stack Overflow
or by George Jempty
Published on 2010-05-20T11:21:20Z
Indexed on
2010/05/20
11:30 UTC
Read the original article
Hit count: 223
Please refer to the code below, when I "comment in" either of the commented out lines, it causes the error (in IE) of "':' expected". So then is my conclusion correct, that this inability to provide a reference to an object value, as an object key in a string literal; is this strictly an interpreter/parsing issue? Is this a candidate for an awful (or at least "bad") "part" of Javascript, in contrast to Crockford's "good parts"?
<script>
var keys = {'ONE': 'one'};
//causes error:
//var obj1 = {keys.ONE: 'value1'};
//var obj1 = {keys['ONE']: 'value1'};
//works
var obj1 = {};
obj1[keys.ONE] = 'value1';
//also works
var key_one = keys.ONE;
var obj2 = {key_one: 'value1'};
</script>
© Stack Overflow or respective owner