javascript/Jquery 将字符串转换成变量名

var a = ['a', 'b',  'c']
var obj = {}
for(i = 0; i < a.length; i++){
    obj[a[i]] = "abc" + 1
}
alert(obj.a)
 alert(obj.b)
  alert(obj.c)
 
如果不想使用obj
可以为
for(i = 0; i < a.length; i++){
    window[a[i]] = "abc" + 1
}
alert(a)
 alert(b)
  alert(c)
但是不推荐这么用,最好是第一种方法。

来源:https://zhidao.baidu.com/question/1366905120567757419.html

猜你喜欢

转载自www.cnblogs.com/WeiYongZhi/p/11584928.html