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.

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]);

Need Your Help

Div with multiple color overlays on hover

html css image hover

I want to create a hover effect on an image that when hovered over multiple colored divs appear. I figure I can do this with CSS, but am having trouble getting the result I want.

SML method that accepts a list list and returns a list

functional-programming sml typing smlnj

I need to create a method that takes a list of lists (like [ [2,3,] , [4,5] ]) and returns something like [2,3,4,5]. I can work out the logic but I dont know how to define the method in SML. I trie...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.