PDA

View Full Version : variable name from array


rsci
05-15-2003, 08:37 PM
Easy question but I can't find anything by searching...

I would like to use components of an array for variable names:


var fDs=new Array("D1","D2","D3");


for (i=0; i<fDs.length; i++) {
var fDs[i]=new reVal(fDs[i])
}

function reVal(name) {
this.name=name+".txt";
this.value=name;
}


In the above 'for' statement I would like to call fDs[i] and use it as a variable name, to be used as a prototype. So, later in the script I can call, for example:

document.write(D1.name);

Easy solution I just can't find it.

Thanks.

cheesebagpipe
05-15-2003, 08:50 PM
var fDs=new Array("D1","D2","D3");

for (i=0; i<fDs.length; i++) {
window[fDs[i]] = new reVal(fDs[i]);
}

function reVal(name) {
this.name=name+".txt";
this.value=name;
}

Danne
05-15-2003, 08:51 PM
You can use the eval-function:


function reVal(name) {
this.name=name+".txt";
this.value=name;
}

var fDs=new Array("D1","D2","D3");


for (i=0; i<fDs.length; i++) {
eval("var "+fDs[i]"=new reVal(\""+fDs[i]+"\");");
}

rsci
05-15-2003, 08:55 PM
The window[] implementation was the simplest and it worked.

Thanks!