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.

2 Answers 2

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]);
Sign up to request clarification or add additional context in comments.

Comments

3

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.