Javascript中对象文字的动态键 [英] dynamic keys for object literals in Javascript
问题描述
好的,所以我正在研究Nodes中的一个项目,我遇到了一个关于对象文字中的键的小问题,我有以下设置:
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.
现在我相信(不确定),对象文字在编译时创建,而不是在运行时创建,这意味着动态变量,如 this.applicationPath
和连接不会是可用:( :(
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.
推荐答案
设置动态密钥的唯一方法是使用括号nota :
The only way you can set dynamic keys is with bracket notation:
required.directories[this.applicationPath + "/configs"] = "Application config folder does not exists";
(当然无论你做什么定义, this.applicationPath
必须存在)
(of course wherever you do this definition, this.applicationPath
must exist)
但你在键中需要 this.applicationPath
吗?你如何访问这些价值观?也许您可以从用于访问属性的任何值中删除 this.applicationPath
。
But do you need this.applicationPath
in the keys? How do you access theses values? Maybe you can just remove this.applicationPath
from whatever value you use to access the properties.
但是如果你需要它:
如果你想避免重复大量的代码,你可以使用数组来初始化密钥: / p>
You could use an array to initialize the keys if you want to avoid repeating a lot of code:
var dirs = ['configs', 'controllers', ...];
var files = ['init.js', 'controllers/index.js', ...];
var required = { directories: {}, files: {} };
required.directories[this.applicationPath] = "Application " + this.application + " does not exists";
for(var i = dirs.length; i--;) {
required.directories[this.applicationPath + '/' + dirs[i]] = "Application " + dirs[i] + " folder does not exists";
}
for(var i = files.length; i--;) {
// same here
}
这篇关于Javascript中对象文字的动态键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!