0

假设我有一个变量 Var123;

var x = "Var";

var VariableMixLOL = x + "123";
//so VariableMixLOL should be equal to Var123, ex. Var123 = "Abc", VariableMixLOL should be "Abc" too

我怎样才能做到这一点?顺便说一句,我正在使用 as3

PS:也添加在标签 JS 因为我认为这是同一件事

4

2 回答 2

3

一种选择是使用eval()

var x = "Var";
var Var123 = "lalaala";
var VariableMixLOL = eval( x + "123" );

另一种选择也是更好的选择是在 JavascriptObject 中对此类事物进行建模。

var x = "variable";
var variables = { "variable123" : "laalala"}; //OR variables = {}; variables["variable123"] = "laalala";

var VariableMixLOL = variables[ x + "123"];
于 2013-07-06T22:35:59.617 回答
1

如果您合并存储变量的对象,则可以使用字符串形式的变量名。

例如:

var x = "Var";
var compoundVar = x + "123";
var obj : Object = {};
obj[compoundVar] = 7;

//Now you can call the variable like this
trace(obj.Var123); //7
于 2013-07-06T22:37:57.377 回答