通过字符串var获取自定义对象属性 [英] Getting a Custom Objects properties by string var
本文介绍了通过字符串var获取自定义对象属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能重复:
javascript对象,访问变量属性名称?
试图在我的JS中获得更高级......
Trying to get more advanced in my JS...
我有一个自定义对象:
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...
谢谢!!!
推荐答案
只需使用 myObject ['thing']
。
这篇关于通过字符串var获取自定义对象属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文