Javascript 按字符串创建对象
这是一个简短的问题,我希望你能帮助我 如何使用字符串导航到对象 如果我有这个:Javascript 按字符串创建对象,javascript,jquery,Javascript,Jquery,这是一个简短的问题,我希望你能帮助我 如何使用字符串导航到对象 如果我有这个: var string = something; 像这样的物体: var this = { something: { other: "okay" } }; this.+string+.other 然后如何使用字符串执行以下操作: var this = { something: { other: "okay" } }; this.+string+
var string = something;
像这样的物体:
var this = {
something: {
other: "okay"
}
};
this.+string+.other
然后如何使用字符串执行以下操作:
var this = {
something: {
other: "okay"
}
};
this.+string+.other
这与:
this.something.other
???也许不是很快,但你知道我要去哪里吗 使用方括号表示法:
var this[something].other
尝试使用下面的
[]
var _this = {
something: {
other: "okay"
}
};
_this[string].other
注意:将变量名称更改为
\u此作为var this=
将抛出错误。另外,此
表示javascript中的当前执行对象/窗口对象。小心使用此
作为变量名。奇怪的事情会发生,或者什么都不会发生+1我还认为使用this
作为变量名不是很好。感谢您的快速回复。这些变量只是为了举例,但我应该更加小心:)