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.
No, JavaScript objects cannot have duplicate keys. The keys must all be unique.
An object can have two same keys or two same values.
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.
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
Prior to ECMAScript 2015 (ed 6), an object literal (ECMAScript calls it an "object initializer") key must be one of:
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"; ...
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With