eval() how else

Discussion in 'Javascript' started by Lee, Aug 31, 2004.

  1. Lee

    Lee Guest

    J. J. Cale said:
    >
    >given that I have a js file included which is written programatically and I
    >can't change it. I would like to know how to do the following using
    >something other than the deprecated eval().
    >whats in the js file
    >var numArrays=something;
    >var data0 = new Array();
    >data0.name="name";
    >data0.data="some data";
    >var data1 = new Array();
    >data1.name="another name";
    >data1.data="some more data";
    >etc ....
    >function getData(arrayName) {
    > for ( var i=0;i<numArrays:i++) {
    > var el=eval('data'+i);
    > if (arrayName = = el.name) doSomething(el.data);
    > }
    >}


    I don't know that eval() is deprecated.
    It's just usually not the best way to do something.

    Assuming that this is in a web page, you can replace:
    eval('data'+i)
    with:
    window['data'+i]

    The fact that the variables are defined in a .js file
    isn't really significant. The contents of that file
    are evaluated in the context of the current window.
     
    Lee, Aug 31, 2004
    #1
    1. Advertising

  2. Lee

    Evertjan. Guest

    J. J. Cale wrote on 31 aug 2004 in comp.lang.javascript:
    > var el=eval('data'+i);


    el = window['data'+i]

    [if a browser is used as javascript base]

    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress,
    but let us keep the discussions in the newsgroup)
     
    Evertjan., Aug 31, 2004
    #2
    1. Advertising

  3. Lee

    Mick White Guest

    J. J. Cale wrote:

    > given that I have a js file included which is written programatically and I
    > can't change it. I would like to know how to do the following using
    > something other than the deprecated eval().
    > whats in the js file
    > var numArrays=something;
    > var data0 = new Array();
    > data0.name="name";
    > data0.data="some data";
    > var data1 = new Array();
    > data1.name="another name";
    > data1.data="some more data";
    > etc ....
    > function getData(arrayName) {
    > for ( var i=0;i<numArrays:i++) {
    > var el=eval('data'+i);

    var el= window['data'+i]
    // Mick

    > if (arrayName = = el.name) doSomething(el.data);
    > }
    > }
    >
    >
    >
     
    Mick White, Aug 31, 2004
    #3
  4. Lee

    J. J. Cale Guest

    given that I have a js file included which is written programatically and I
    can't change it. I would like to know how to do the following using
    something other than the deprecated eval().
    whats in the js file
    var numArrays=something;
    var data0 = new Array();
    data0.name="name";
    data0.data="some data";
    var data1 = new Array();
    data1.name="another name";
    data1.data="some more data";
    etc ....
    function getData(arrayName) {
    for ( var i=0;i<numArrays:i++) {
    var el=eval('data'+i);
    if (arrayName = = el.name) doSomething(el.data);
    }
    }
     
    J. J. Cale, Aug 31, 2004
    #4
  5. J. J. Cale wrote:

    > given that I have a js file included which is written programatically and
    > I can't change it.


    Why?

    > I would like to know how to do the following using something other than
    > the deprecated eval().
    > whats in the js file
    > var numArrays=something;
    > var data0 = new Array();
    > data0.name="name";
    > data0.data="some data";
    > var data1 = new Array();
    > data1.name="another name";
    > data1.data="some more data";
    > etc ....
    > function getData(arrayName) {
    > for ( var i=0;i<numArrays:i++) {
    > var el=eval('data'+i);
    > if (arrayName = = el.name) doSomething(el.data);
    > }
    > }


    var _global = this;

    function getData(arrayName)
    {
    for (var i = 0; i < numArrays; i++)
    {
    var el = _global['data' + i];
    if (arrayName == el.name)
    {
    doSomething(el.data);
    }
    }
    }

    But:
    Why do you not use an Array object in the first place?
    Why do data0, data1 aso. refer to an Array object anyway?

    var data = [
    {name: "name",
    data: "some data"},
    {name: "another name",
    data: "some more data"}];

    Or define and use a prototype:

    function Data(sName, d)
    {
    this.name = sName;
    this.data = d;
    }

    var data = [
    new Data("name", "some data"),
    new Data("another name", "some more data")];


    PointedEars

    P.S.
    Please use empty lines in your postings for
    paragraphs to make them easier legible.
    --
    Our only hope lies in the mind of Kiki
     
    Thomas 'PointedEars' Lahn, Sep 4, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    25
    Views:
    671
    MonkeeSage
    Oct 1, 2006
  2. kj
    Replies:
    15
    Views:
    559
    Lawrence D'Oliveiro
    May 23, 2009
  3. A
    Replies:
    8
    Views:
    809
    James Kanze
    Aug 28, 2010
  4. Gergely Kontra
    Replies:
    16
    Views:
    232
    Devin Mullins
    Sep 20, 2005
  5. Bohne

    eval or something else

    Bohne, Sep 21, 2006, in forum: Perl Misc
    Replies:
    2
    Views:
    83
    Bohne
    Sep 21, 2006
Loading...

Share This Page