By George Jempty


2010-05-20 11:21:20 8 Comments

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>

4 comments

@Aamir Afridi 2012-08-14 15:29:17

You can try:

var obj = {};
var val = 'foo';

obj[val] = 'bar'; 

obj.foo >>> 'bar';

@naugtur 2010-05-20 11:26:06

You cannot use variables as keys when defining object with {}

Therefore they are being interpreted as string names and can consist only of characters avaliable for variable names

the

objectname[anythingThatReturnsValue]='value1'; is the way to go.

ALSO

You can generate a string and parse it

var s='{"'+keys.ONE+'": "value1"}';
var obj=JSON.parse(s);
//or
s='var obj2='+s;
eval(s);

Both above methods are bad practices for creating objects in JavaScript and I don't recommend them.

@George Jempty 2010-05-20 11:28:13

You CAN use variables as keys when defining objects with {}, see the second "also works" block.

@Guffa 2010-05-20 11:31:55

@George: No, you can't. The code runs, but it does not use the variable in the literal object.

@Salman A 2010-05-20 11:31:59

@George: He means its a syntax error and there isn't anything you can do about it :)

@naugtur 2010-05-20 11:34:02

@George - to be clear: var obj2 = {key_one: 'value1'}; is the same as var obj2 = {"key_one": 'value1'}; - key_one will be the name of the field.

@T.J. Crowder 2010-05-20 11:35:31

@George: Clarifying Guffa's reply: The key_one in in the second "also works" example is the name of the key; if you examine the object, it will have a property called "key_one", not "one" as it would if the value of key_one were being used when you created the literal.

@George Jempty 2010-05-20 11:38:46

acknowledged, thanks

@vol7ron 2012-06-04 21:30:50

@naugtur: For JSON, I think you may have to also quote the key var s = '{" + keys.ONE + '":"value1"}';

@naugtur 2012-06-06 12:06:33

@vol7ron Right, fixed

@vol7ron 2012-06-06 13:15:05

Didn't mean to bring up an old answer, just saw this passing by :)

@naugtur 2012-06-06 16:14:49

It's old, but I'm always glad to fix an old error ;)

@jpsimons 2010-05-20 11:44:16

Think about it: if it were to work the way you want it would totally introduce a language ambiguity.

var obj = {something:"red"}
var obj = {"something":"red"}

The two statements are equivalent in JavaScript, because bareword keys are "autoquoted." So if something means the literal string "something", how it could also refer to the variable "something". It can't. So if you want to use variables they have to go in square bracket notation instead of key : value notation.

@Guffa 2010-05-20 11:28:47

The limitation of the literal object syntax is that the names has to be literal. As the names can be specified as an identifer as well as a string, it's not possible to use a variable instead.

This will create an object with a property n, not a property answer:

var n = 'answer';
var o = { n: 42 };

@Mathias Bynens 2012-03-06 09:23:09

Numeric literals are valid property names as well. Also, in ES5, the names can be specified as an identifier name (i.e., not just identifiers, but also reserved words). See stackoverflow.com/a/9568622/96656.

Related Questions

Sponsored Content

36 Answered Questions

[SOLVED] How do I remove a property from a JavaScript object?

18 Answered Questions

[SOLVED] Checking if a key exists in a JavaScript object?

64 Answered Questions

[SOLVED] What is the most efficient way to deep clone an object in JavaScript?

42 Answered Questions

[SOLVED] How do I check if an array includes an object in JavaScript?

33 Answered Questions

[SOLVED] How do I loop through or enumerate a JavaScript object?

36 Answered Questions

[SOLVED] Length of a JavaScript object

16 Answered Questions

[SOLVED] Parse JSON in JavaScript?

42 Answered Questions

[SOLVED] How do I test for an empty JavaScript object?

  • 2009-03-25 01:39:45
  • falmp
  • 1407649 View
  • 1985 Score
  • 42 Answer
  • Tags:   javascript json

22 Answered Questions

[SOLVED] How can I add a key/value pair to a JavaScript object?

55 Answered Questions

[SOLVED] How do I correctly clone a JavaScript object?

  • 2009-04-08 03:01:06
  • mindeavor
  • 1473124 View
  • 2499 Score
  • 55 Answer
  • Tags:   javascript clone

Sponsored Content