Getting a Custom Objects properties by string var

2019-01-22 05:02发布

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

2条回答
时光不老,我们不散
2楼-- · 2019-01-22 05:43

Simply use myObject['thing'].

查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-01-22 05:47

You could use:

myObject[propertyString] ;
查看更多
登录 后发表回答