Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling a variable given its string name

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.

like image 720
sameold Avatar asked Mar 27 '26 23:03

sameold


2 Answers

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]);
like image 121
taskinoor Avatar answered Mar 30 '26 09:03

taskinoor


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

like image 23
kapex Avatar answered Mar 30 '26 09:03

kapex



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!