Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dynamic keys for object literals in Javascript [duplicate]

Ok so I'm working away on a project in Nodes, and I've come across a small problem with the keys in object literals, I have the following set-up:

var required = {     directories : {         this.applicationPath                    : "Application " + this.application + " does not exists",         this.applicationPath + "/configs"       : "Application config folder does not exists",         this.applicationPath + "/controllers"   : "Application controllers folder does not exists",         this.applicationPath + "/public"        : "Application public folder does not exists",         this.applicationPath + "/views"         : "Application views folder does not exists"     },     files : {         this.applicationPath + "/init.js"               : "Application init.js file does not exists",         this.applicationPath + "/controllers/index.js"  : "Application index.js controller file does not exists",         this.applicationPath + "/configs/application.js": "Application configs/application.js file does not exists",         this.applicationPath + "/configs/server.js"     : "Application configs/server.js file does not exists"     } } 

Ok so many of you will look at this and think it look's OK, but the compiler keeps telling me that I am missing a : (colon), which im not, it seems like the + or and the . are both effecting the compiler.

Now i believe (not sure), that object literals are created at compile time, and not run-time, meaning that dynamic variables such as this.applicationPath and concatenation are not going to be available :( :(

What's the best way to overcome an obstacle like this without having to rewrite large chunks of code.

like image 561
RobertPitt Avatar asked Jun 28 '11 01:06

RobertPitt


People also ask

Can object in JavaScript have duplicate keys?

No, JavaScript objects cannot have duplicate keys. The keys must all be unique.

Can an object key have multiple values JavaScript?

An object can have two same keys or two same values.

What is JavaScript object literal?

A JavaScript object literal is a comma-separated list of name-value pairs wrapped in curly braces. Object literals encapsulate data, enclosing it in a tidy package. This minimizes the use of global variables which can cause problems when combining code.


2 Answers

Computed property names are supported in ECMAScript2015:

var name = 'key';  var value = 'value';  var o = {    [name]: value  };  alert("o as json : " + JSON.stringify(o));

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer

like image 139
Rich Apodaca Avatar answered Sep 28 '22 07:09

Rich Apodaca


Prior to ECMAScript 2015 (ed 6), an object literal (ECMAScript calls it an "object initializer") key must be one of:

  1. IdentifierName
  2. StringLiteral
  3. NumericLiteral

So you couldn't use an expression as the key in an initialiser. This was changed as of ECMAScript 2015 (see below). You could use an expression with square bracket notation to access a property, so to set the properties with an expression you had to do:

var required = { directories : {}}; required.directories[this.applicationPath] = "Application " + this.application + " does not exists"; required.directories[this.applicationPath + "/configs"] = "Application config folder does not exists"; ... 

and so on. Since this.applicationPath is reused a lot, better to store a reference to help with performance and cut down the amount of code:

var a = this.applicationPath; var required = { directories : {}}; var rd = required.directories; rd[a] = "Application " + this.application + " does not exists"; rd[a + "/configs"] = "Application config folder does not exists"; ... 

Edit

As of ECMAScript 2015 (ed 6), object initializers can have computed keys using:

[expression]: value 

There is also shorthand syntax for property and method names.

See MDN: Object Initializer or ECMAScript Object Initializer.

like image 22
RobG Avatar answered Sep 28 '22 07:09

RobG