<script>
var Thread_num=5
for(var i=1i<=Thread_numi++){
eval("var set_" + i + "=" + i)
}
alert(set_5)
</script>
2.方法1占内存,建议用数组代替
<script>
var Thread_num=5
var arr=[]
for(var i=1i<=Thread_numi++){
arr[i] = i
}
</script>
<!DOCTYPE HTML><html>
<head>
<meta charset=UTF-8>
<title>YuGiOh</title>
<style type="text/css">
</style>
<script type="text/javascript">
var settings = {}
for ( var i = 1 i < 101 i++)
{
settings["nav" + i] = "left" + i
}
alert (settings.nav100)
</script>
</head>
<body>
</body>
</html>
动态生成全局变量://简单的用字符串作为变量名
window['hello'] = "hello, world"
http://r.yuzhua.com
alert(hello)//批量定义for(var i=0i<10i++) { var varname="var"+i window[varname] = "value"+i}alert(var0)alert(var9)
http:www.baidu.com
解释:所有的全局变量都存在window变量里。window是个js本身定义的变量,类型为object。
访问全局变量var0 相当于访问window.var0,也相当于window["var0"]。
局部变量最好用object吧:http:www.baidu.com
function test() { var vars = {} // 简单的字符串作为变量名 vars['hello'] = "hello, world!" alert(vars.hello)//批量定义 for(var i=0i<10i++) {var varname="var"+i vars[varname] = "value"+i } alert(vars.var0) alert(vars.var9)