假设我有一个变量 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 因为我认为这是同一件事
假设我有一个变量 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 因为我认为这是同一件事
一种选择是使用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"];
如果您合并存储变量的对象,则可以使用字符串形式的变量名。
例如:
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