Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to refer to object properties dynamically?

Status
Not open for further replies.

dcnguyen

Technical User
Mar 22, 2005
54
US
Let's say I have a Person object with properties age, name, and height I have a function called adjustProperty that takes in two paramaters, one is a reference to a Person's property and then to a value to change that property to...so if I do this:

Tom.adjustProperty("age", "20");

It changes Tom's age to 20...

So...how do I actually refer to the property inside the function call, if it is defined as so:

function adjustProperty(prop, val){

this.prop = val; //i take it, this does NOT work
}
 
Try:
Code:
this[prop] = val;

You can access object properties and methods as an associative array as well as with the dot notation.

Lee
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top