通过字符串 var 获取自定义对象属性 [英] Getting a Custom Objects properties by string var
问题描述
可能的重复:
javascript对象,访问变量属性名?
试图在我的 JS 中变得更高级...
我有一个自定义对象:
Object myObject = new Object();myObject.thing = 任何东西在这里;
我希望能够通过传入一个字符串来检索自定义对象属性...例如:
var propertyString = '东西';警报(myObject.propertyString);
我不太明白.我已经查看了许多自定义对象的教程 - 但没有任何内容显示如何获取我不知道名称的属性......此外 - 如果可能,我想避免遍历所有属性......>
谢谢!!!
只需使用myObject['thing']
.
Possible Duplicate:
javascript object, access variable property name?
Trying to get more advanced in my JS...
I have a custom object:
Object myObject = new Object();
myObject.thing = anythingHere;
I would like to be able to retrieve a custom objects property by passing in a string... eg:
var propertyString = 'thing';
alert(myObject.propertyString);
I can't quite figure that out. I've looked at a number of tutorials for custom objects - but nothing shows how to get properties that I don't know the names of... Also - I would like to avoid looping through all properties if possible...
Thanks!!!
Simply use myObject['thing']
.
这篇关于通过字符串 var 获取自定义对象属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!