3
var myvar = "this is the value of myvar";
var notMyvar = "this is some other variable";
var thirdVar = "this is some third var";

var nameOfVarToCall = "myvar";
//print the content of the var name specified, in this case "myvar"

I have a string which contains the name of a variable I need. What I want to do is find out the name of var (in this case "myvar") and use the name to access the value of the var (in this case just print out the value). Is this feature available in Actionscript. I know it's available in PHP and can come in very handy.

sameold
  • 18,400
  • 21
  • 63
  • 87

2 Answers2

8

You can access any property of the object specified as string by using [] operator.

// assuming nameOfVarToCall is a member of this object.
trace(this[nameOfVarToCall]);
// if nameOfVarToCall is member of object myObj
trace(myObj[nameOfVarToCall]);
taskinoor
  • 45,586
  • 12
  • 116
  • 142
3

In a class context, if you have a property named myVar, you can use this["myVar"], as alternative to this.myVar.

kapex
  • 28,903
  • 6
  • 107
  • 121