说我想要这样的东西:
var i = 0;
for (i upto 100) {
var x+i = "This is another variable altogether";
i++;
}
javascript解释器将看到:
var x = "This is another variable altogether";
var x1 = "This is another variable altogether";
var x2= "This is another variable altogether";
var x3 = "This is another variable altogether";
var x4 = "This is another variable altogether";
我可以使用变量计数器来增加变量的名称,以便完全拥有不同的变量吗?
最佳答案
从技术上讲,是的。
如果您怀疑自己是在全球范围内运营,则可以执行以下操作:
for (var i=0; i<100; i++){
window["x"+i]="This is another variable altogether";
}
console.log(x1);
但是,您应该使用数组来做这种事情,所以:
var arr=[];
for (var i=0; i<100; i++){
arr[i]="This is another variable altogether";
}
console.log(arr[1]);