Dynamic variables

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Antonie C Malan Snr

    Dynamic variables

    Dear All,

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

    This doesn't:

    function testEval(){
    var FordModels = new Array('Fiesta', 'Mondeo','Falco n');
    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

  • Martin Honnen

    #2
    Re: Dynamic variables



    Antonie C Malan Snr wrote:

    [color=blue]
    > function testEval(){
    > var FordModels = new Array('Fiesta', 'Mondeo','Falco n');
    > 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.[/color]

    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("carFordmo dels=Fiesta,Mon deo,Falcon")
    and then the scripting engine rightly complaints that Fiesta is undefined.

    --

    Martin Honnen

    Comment

    • Lee

      #3
      Re: Dynamic variables

      Antonie C Malan Snr said:[color=blue]
      >
      >Dear All,
      >
      >In short, this works:
      >function testEval(){
      > var veh = "car";
      > eval(veh + "Ford" + "models" + "= new
      >Array('Fiesta' ,'Mondeo','Falc on');");
      > for(i = 0; i < 3; i++){
      > alert("The model: " + carFordmodels[i]);
      > }
      > }
      >
      >This doesn't:
      >
      >function testEval(){
      > var FordModels = new Array('Fiesta', 'Mondeo','Falco n');
      > var veh = "car";
      > eval(veh + "Ford" + "models" + "=" + FordModels);
      > for(i = 0; i < 3; i++){
      > alert("The model: " + carFordmodels[i]);
      > }
      > }[/color]


      var dynamic=new Object();
      function doNotUseEvalFor DynamicVariable s() {
      var FordModels = ['Fiesta','Monde o','Falcon'];
      var veh = "car";
      dynamic[veh + "Ford" + "models"] = FordModels;
      for(i = 0; i < 3; i++){
      alert("The model: " + dynamic.carFord models[i]);
      }
      }

      Comment

      • Douglas Crockford

        #4
        Re: Dynamic variables

        Antonie C Malan Snr wrote:
        [color=blue]
        > Dear All,
        >
        > In short, this works:
        > function testEval(){
        > var veh = "car";
        > eval(veh + "Ford" + "models" + "= new
        > Array('Fiesta', 'Mondeo','Falco n');");
        > for(i = 0; i < 3; i++){
        > alert("The model: " + carFordmodels[i]);
        > }
        > }
        >
        > This doesn't:
        >
        > function testEval(){
        > var FordModels = new Array('Fiesta', 'Mondeo','Falco n');
        > 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.[/color]

        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.Fordmod els;
        for (i = 0; i < models.length; i += 1) {
        alert("The model: " + models[i]'
        }


        Comment

        • Antonie Malan

          #5
          Re: Dynamic variables



          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!

          Comment

          Working...