在另一个对象中重用对象
我有一个对象,其中包含另一个带有翻译的对象。它看起来像这样:
var parent = { //parent object with a bunch of stuff in it
countryAlerts:{ //area for country alerts
"USA":{ //country
en:"FAST", //alert text in English
es:"RAPIDO" //alert text in Spanish
},
"Japan":{
en:"FAST",
es:"RAPIDO"
}
}
}
我想做这样的事情:
var parent = {
countryAlerts:{
"USA":fast,
"Japan":fast
}
}
var fast = {
en:"FAST",
es:"RAPIDO"
}
但我似乎无法让它工作。我想引用这样的对象:parent.countryAlerts.USA.LANGUAGE
。
任何帮助表示赞赏。
谢谢!
I have an object that contains another object with translations. It looks like this:
var parent = { //parent object with a bunch of stuff in it
countryAlerts:{ //area for country alerts
"USA":{ //country
en:"FAST", //alert text in English
es:"RAPIDO" //alert text in Spanish
},
"Japan":{
en:"FAST",
es:"RAPIDO"
}
}
}
I would like to do something like this:
var parent = {
countryAlerts:{
"USA":fast,
"Japan":fast
}
}
var fast = {
en:"FAST",
es:"RAPIDO"
}
I can't seem to get it to work though. I would like to refer to the object like this: parent.countryAlerts.USA.LANGUAGE
.
Any help is appreciated.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,这是可能的,您只需首先定义
fast
:请注意使用方括号表示法通过变量名称访问对象的成员。
编辑:回应评论:
所以你的意思是,像这样(伪代码)?
简而言之,你不能用这样的一句话来做到这一点。最好的选择是将其移至多个语句。
Yes that is possible, you just have to define
fast
first:Note the use of the square bracket notation for accessing an object's members by a variable name.
Edit: In response to the comment:
So you mean, like this (pseudo-code)?
In short, you can't do it in one statement like that. Your best bet would be to move it to multiple statements.
为了您的后续行动:
For your follow-up: