Skip to main content
jake_flex
Participant
August 17, 2009
Question

How to get attribute/variable name at runtime?

  • August 17, 2009
  • 1 reply
  • 833 views

How would I get the name of the attribute/varialbe at runtime? For example if I have a variable defined like

var abc:String = '123';

From this, I would like to get the variable name, which is 'abc'.

Thanks.

This topic has been closed for replies.

1 reply

kglad
Community Expert
Community Expert
August 17, 2009

how do you know which variable name you want to "get"?

jake_flex
jake_flexAuthor
Participant
August 18, 2009

Thanks for your reply kglad.

My question was done in a hurry, and left too many things open. Sorry for that. I'll try ask it again in better manner.

String is not a good example even. Object is better, since it's more generic. Let's assume I have a code like this

var obj:Object = new Object();
obj.a = 1;          // These attributes are added at runtime
obj.b = 2;
obj.c = 3;

Object itself does not have any attributes, only the ones I add. Now my question is, is there a way to get a list of the attribute names? And also get the values of each attribute with that name?

I noticed that the Object has a method hasOwnProperty(name:String) which returns a boolean if the object has a property with the given name. This does not really help, since I don't always know the attribute names, and there could n number of them.

Thanks.

P.S. Are they attributes or properties in AS? Or are both terms OK? Now I understand that the name 'variable' which I typed in the subject is not correct.

jake_flex
jake_flexAuthor
Participant
August 18, 2009

And it was this easy

var j:String;

for(j in obj)
      Alert.show(obj+":"+j);