Getting a Custom Objects properties by string var [duplicate]

China☆狼群 提交于 2019-11-27 11:51:01

问题


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!!!


回答1:


Simply use myObject['thing'].




回答2:


You could use:

myObject[propertyString] ;


来源:https://stackoverflow.com/questions/7068968/getting-a-custom-objects-properties-by-string-var

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!