字符串转变量可以使用eval(aaa)
,
判断字符串对应的变量名是否存在: if(eval('typeof ' + "aaa") != "undefined") { ...}
举例:
var v1 = 100;
var v2 = 200 ;
console.log(eval("v1"));
console.log(eval("v2"));
if(eval('typeof ' + "v3") != "undefined"){
console.log(eval("v3"));
}
参考: https://stackoverflow.com/questions/24001442/javascript-eval-referenceerror-is-not-defined