Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this Javascript object literal key restriction strictly due to parsing?

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>
like image 568
Dexygen Avatar asked May 20 '10 11:05

Dexygen


People also ask

What does object literal mean in JavaScript?

Using an Object Literal This is the easiest way to create a JavaScript Object. Using an object literal, you both define and create an object in one statement. An object literal is a list of name:value pairs (like age:50) inside curly braces {}.

Is there a way to use variable keys in a JavaScript object literal?

ES6 defines 'ComputedPropertyName' as part of the grammar for object literals, which helps use a variable for a key. Object keys can be dynamically assigned in ES6 by placing an expression in square brackets. Syntax: var key="your_choice"; var object = {}; object[key] = "your_choice"; console.

What is the correct syntax for declaring an object literal?

Declaring methods and properties using Object Literal syntax The Object literal notation is basically an array of key:value pairs, with a colon separating the keys and values, and a comma after every key:value pair, except for the last, just like a regular array.

What is object literal in ES6?

Object Property Initializer Before ES6, the object literal is a collection of name-value pairs. For example, In ES5. function user(name, division) { return {


3 Answers

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 };
like image 109
Guffa Avatar answered Oct 23 '22 02:10

Guffa


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.

like image 21
naugtur Avatar answered Oct 23 '22 01:10

naugtur


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.

like image 2
jpsimons Avatar answered Oct 23 '22 01:10

jpsimons