提问人:RobertPitt 提问时间:6/28/2011 最后编辑:Jonathan LefflerRobertPitt 更新时间:10/16/2020 访问量:58157
Javascript 中对象文字的动态键 [duplicate]
dynamic keys for object literals in Javascript [duplicate]
问:
好的,所以我正在 Nodes 中处理一个项目,并且我遇到了对象字面量中的键的小问题,我有以下设置:
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"
}
}
好的,你们中的很多人会看这个并认为它看起来没问题,但是编译器一直告诉我我缺少一个(冒号),而事实并非如此,似乎 or 和 the 都在影响编译器。:
+
.
现在我相信(不确定),对象文字是在编译时创建的,而不是在运行时创建的,这意味着动态变量(如和串联)将:( :(不可用this.applicationPath
克服这样的障碍而不必重写大量代码的最佳方法是什么?
答:
问题在于使用“this”,因为它不是指任何智能*。 创建包含 applicationPath 的静态文本。
var required={ "applicationPath":"someWhereOverTheRainboW" };
然后使用
required.directories={}; required.directories[required.applicationPath + "/configs"]="Application config folder does not exists"; ....
动态填充
编辑; 我匆匆忙忙地提出了我的第一个想法,但没有奏效。以上现在有效 - 对不起!
* 关键字“this”非常聪明:)但它通常指的是窗口对象或元素、事件已被触发或称为“活动”对象。因此,造成很多混乱;)
评论
对于对象文字,Javascript/ECMAScript 脚本将键指定为有效的 IdentifierName、字符串文字或数字信用 RobG(甚至十六进制)。不是表达式,这就是。required.applicationPath + "/configs"
评论
this.applicationPath
this
您可以使用括号表示法设置动态键:
required.directories[this.applicationPath + "/configs"] = "Application config folder does not exists";
(当然,无论你在哪里做这个定义,都必须存在)this.applicationPath
但是你需要钥匙吗?如何访问这些值?也许你可以从你用来访问属性的任何值中删除。this.applicationPath
this.applicationPath
但如果你需要它:
如果你想避免重复大量代码,你可以使用数组来初始化密钥:
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
}
评论
{ ...Object.defineProperty({}, key, { value, enumerable: true }) }
{ [key]: value }
在 ECMAScript 2015 (ed 6) 之前,对象字面量(ECMAScript 称其为“对象初始值设定项”)键必须是以下项之一:
- 标识符名称
- StringLiteral(字符串文字)
- NumericLiteral
因此,您不能将表达式用作初始值设定项中的键。自 ECMAScript 2015 起,此情况已更改(见下文)。您可以使用带有方括号表示法的表达式来访问属性,因此要使用表达式设置属性,您必须执行以下操作:
var required = { directories : {}};
required.directories[this.applicationPath] = "Application " + this.application + " does not exists";
required.directories[this.applicationPath + "/configs"] = "Application config folder does not exists";
...
等等。由于被大量重用,因此最好存储引用以帮助提高性能并减少代码量:this.applicationPath
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";
...
编辑
从 ECMAScript 2015 (ed 6) 开始,对象初始值设定项可以使用以下命令计算键:
[expression]: value
还有属性和方法名称的简写语法。
参见 MDN:对象初始值设定项或 ECMAScript 对象初始值设定项。
评论
如果你有一个深层的对象结构(比如 Grunt 配置),有时能够使用 Felix 概述的括号表示法返回动态生成的对象键,但在对象结构中内联是很方便的。这可以通过使用函数在深层对象的上下文中动态返回对象来实现;对于此问题中的代码,如下所示:
var required = {
directories : function() {
var o = {};
o[this.applicationPath] = "Application " + this.application + " does not exists";
o[this.applicationPath + "/configs"] = "Application config folder does not exists";
o[this.applicationPath + "/controllers"] = "Application controllers folder does not exists";
o[this.applicationPath + "/public"] = "Application public folder does not exists";
o[this.applicationPath + "/views"] = "Application views folder does not exists";
return o;
}(),
files : function() {
var o = {};
o[this.applicationPath + "/init.js"] = "Application init.js file does not exists";
o[this.applicationPath + "/controllers/index.js"] = "Application index.js controller file does not exists";
o[this.applicationPath + "/configs/application.js"] ="Application configs/application.js file does not exists";
o[this.applicationPath + "/configs/server.js"] ="Application configs/server.js file does not exists";
return o;
}()
}
这把小提琴验证了这种方法。
受到 babel 如何将新的 ES6 语法 () 转换为旧 Javascript 的启发,我了解到你可以用一句话来做到这一点:{[expression]: value}
var obj = (_obj = {}, _obj[expression] = value, _obj);
例:
var dynamic_key = "hello";
var value = "world";
var obj = (_obj = {}, _obj[dynamic_key] = value, _obj);
console.log(obj);
// Object {hello: "world"}
(在最新的 Chrome 上测试)
一个老问题,当时的答案是正确的,但时代变了。如果有人在谷歌搜索中挖掘它,新的 javascript 版本 (ES6) 允许使用表达式作为对象文字的键,如果它们被方括号括起来:var obj={["a"+Math.PI]:42}
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
评论