By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,940 Members | 1,522 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,940 IT Pros & Developers. It's quick & easy.

Dynamic variables

P: n/a
Dear All,

In short, this works:
function testEval(){
var veh = "car";
eval(veh + "Ford" + "models" + "= new
Array('Fiesta','Mondeo','Falcon');");
for(i = 0; i < 3; i++){
alert("The model: " + carFordmodels[i]);
}
}

This doesn't:

function testEval(){
var FordModels = new Array('Fiesta','Mondeo','Falcon');
var veh = "car";
eval(veh + "Ford" + "models" + "=" + FordModels);
for(i = 0; i < 3; i++){
alert("The model: " + carFordmodels[i]);
}
}

The browser (two of them) thinks Fiesta is an undefined variable. How
do I get the second example to work, i.e. where there is a reference to
an Array of Strings. I also tried eval(veh + "Ford" + "models") =
FordModels; but no luck.

I'm quite stuck.

Thanks in advance.

Chris

Jul 23 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a


Antonie C Malan Snr wrote:

function testEval(){
var FordModels = new Array('Fiesta','Mondeo','Falcon');
var veh = "car";
eval(veh + "Ford" + "models" + "=" + FordModels);
for(i = 0; i < 3; i++){
alert("The model: " + carFordmodels[i]);
}
}

The browser (two of them) thinks Fiesta is an undefined variable.


I am sure you don't need all those evals but I think what you are
looking for is
eval(veh + "Ford" + "models" + "= FordModels;");
as what you have yields
eval("carFordmodels=Fiesta,Mondeo,Falcon")
and then the scripting engine rightly complaints that Fiesta is undefined.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #2

P: n/a
Lee
Antonie C Malan Snr said:

Dear All,

In short, this works:
function testEval(){
var veh = "car";
eval(veh + "Ford" + "models" + "= new
Array('Fiesta','Mondeo','Falcon');");
for(i = 0; i < 3; i++){
alert("The model: " + carFordmodels[i]);
}
}

This doesn't:

function testEval(){
var FordModels = new Array('Fiesta','Mondeo','Falcon');
var veh = "car";
eval(veh + "Ford" + "models" + "=" + FordModels);
for(i = 0; i < 3; i++){
alert("The model: " + carFordmodels[i]);
}
}

var dynamic=new Object();
function doNotUseEvalForDynamicVariables() {
var FordModels = ['Fiesta','Mondeo','Falcon'];
var veh = "car";
dynamic[veh + "Ford" + "models"] = FordModels;
for(i = 0; i < 3; i++){
alert("The model: " + dynamic.carFordmodels[i]);
}
}

Jul 23 '05 #3

P: n/a
Antonie C Malan Snr wrote:
Dear All,

In short, this works:
function testEval(){
var veh = "car";
eval(veh + "Ford" + "models" + "= new
Array('Fiesta','Mondeo','Falcon');");
for(i = 0; i < 3; i++){
alert("The model: " + carFordmodels[i]);
}
}

This doesn't:

function testEval(){
var FordModels = new Array('Fiesta','Mondeo','Falcon');
var veh = "car";
eval(veh + "Ford" + "models" + "=" + FordModels);
for(i = 0; i < 3; i++){
alert("The model: " + carFordmodels[i]);
}
}

The browser (two of them) thinks Fiesta is an undefined variable. How
do I get the second example to work, i.e. where there is a reference to
an Array of Strings. I also tried eval(veh + "Ford" + "models") =
FordModels; but no luck.


Cheese and crackers! That is about the worst possible way to do that.
Objects do this much much more nicely. And here's a tip: If you are
using eval, you are doing it wrong.

var veh = {car: {}, truck: {}};
veh['car']["Ford" + "models"] = ['Fiesta', 'Mondeo', 'Galcon'];
var models = veh.car.Fordmodels;
for (i = 0; i < models.length; i += 1) {
alert("The model: " + models[i]'
}

http://www.crockford.com/javascript/survey.html
Jul 23 '05 #4

P: n/a


Chris Malan

Thanks guys. Now it works. What I wanted to do is create any number of
JavaScript arrays from data in the database. The array names are
dynamic, but can be got at from data in the selects. E.g. if somebody
wants to see all the Fords in the Category car the name of the
modelsArray will be carFordModels. As I don't know which makes are
going to be in the database I could think of no other way to do it. I
don't want a round trip for every select value selection to see what the
next select valid values are.

Thanks,

Chris

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.