dynamic variables

Discussion in 'Javascript' started by J.P., May 6, 2005.

  1. J.P.

    J.P. Guest

    Hi,

    I need to create a variable out of nothing. From a database I extract an
    item with a certain id. With this id I want to create a new variable.
    For example:

    id = 36;

    "item"+id = new Array();

    Now I get the message "Illegal left hand assignment".

    I tried:

    eval("item"+id) = new Array();

    Is it possible to create a variable out of nothing?

    ---

    J.P.
     
    J.P., May 6, 2005
    #1
    1. Advertising

  2. J.P.

    Lee Guest

    J.P. said:
    >
    >Hi,
    >
    >I need to create a variable out of nothing. From a database I extract an
    >item with a certain id. With this id I want to create a new variable.
    >For example:
    >
    >id = 36;
    >
    >"item"+id = new Array();
    >
    >Now I get the message "Illegal left hand assignment".
    >
    >I tried:
    >
    >eval("item"+id) = new Array();
    >
    >Is it possible to create a variable out of nothing?


    var dynamic = new Object();
    dynamic["item"+id] = new Array();
    dynamic["item"+id][0] = "somevalue";
     
    Lee, May 6, 2005
    #2
    1. Advertising

  3. On 06/05/2005 21:05, J.P. wrote:

    > I need to create a variable out of nothing. From a database I extract an
    > item with a certain id. With this id I want to create a new variable.
    > For example:
    >
    > id = 36;
    >
    > "item"+id = new Array();


    [snip]

    There are two options that immediately spring to mind. They're basically
    the same, but I prefer the latter.

    1) Create these variables on the global object:

    /* In global scope: */
    var global = this;

    /* ... */

    /* In any scope: */
    global['item' + id] = []; /* Create new array */

    which can also be written:

    window['item' + id] = [];

    2) Create an object that will do the same thing as the global
    object, above:

    var items = {}; /* Create an object, items
    * {} is an object literal,
    * equivalent to new Object()
    */

    /* ... */

    items[id] = []; // Create new array


    The notation, identifier[...], has nothing to do with arrays in this
    situation. Square bracket notation, as it is known, is effectively the
    same as regular dot notation used to access object members, except that
    it allows the use of expressions to compose the property name. See the
    relevant FAQ notes article[1] for more information.

    Hope that helps,
    Mike


    [1] <URL:http://www.jibbering.com/faq/faq_notes/square_brackets.html>

    --
    Michael Winter
    Replace ".invalid" with ".uk" to reply by e-mail.
     
    Michael Winter, May 6, 2005
    #3
  4. J.P.

    J.P. Guest

    Michael Winter schreef:
    <remove>

    > 2) Create an object that will do the same thing as the global
    > object, above:
    >
    > var items = {}; /* Create an object, items
    > * {} is an object literal,
    > * equivalent to new Object()
    > */
    >
    > /* ... */
    >
    > items[id] = []; // Create new array
    >
    >
    > The notation, identifier[...], has nothing to do with arrays in this
    > situation. Square bracket notation, as it is known, is effectively the
    > same as regular dot notation used to access object members, except that
    > it allows the use of expressions to compose the property name. See the
    > relevant FAQ notes article[1] for more information.
    >
    > Hope that helps,
    > Mike
    >
    >
    > [1] <URL:http://www.jibbering.com/faq/faq_notes/square_brackets.html>
    >


    Yes, that really helps. Thanks a lot!! My code is readable again!

    --

    J.P.
     
    J.P., May 6, 2005
    #4
  5. J.P.

    RobG Guest

    J.P. wrote:
    > Michael Winter schreef:
    > <remove>
    >
    >> 2) Create an object that will do the same thing as the global
    >> object, above:
    >>
    >> var items = {}; /* Create an object, items
    >> * {} is an object literal,
    >> * equivalent to new Object()
    >> */
    >>
    >> /* ... */
    >>
    >> items[id] = []; // Create new array
    >>
    >>
    >> The notation, identifier[...], has nothing to do with arrays in this
    >> situation. Square bracket notation, as it is known, is effectively the
    >> same as regular dot notation used to access object members, except
    >> that it allows the use of expressions to compose the property name.
    >> See the relevant FAQ notes article[1] for more information.
    >>
    >> Hope that helps,
    >> Mike
    >>
    >>
    >> [1] <URL:http://www.jibbering.com/faq/faq_notes/square_brackets.html>
    >>

    >
    > Yes, that really helps. Thanks a lot!! My code is readable again!
    >


    To pre-empt your next question "How do I see what's in the object?"

    Here's how:

    var item = {}
    item[0] = 'foo';
    item[1] = ['bar','buzz'];

    var msg='Properties of object item:\n';
    for ( prop in item) {
    msg += '\n' + prop + ' has value ' + item[prop];
    }

    alert(msg);

    Another interesting feature is that you can add methods to your
    object. So you could add a method that shows what's in the object:

    item.showContent = function() {
    var msg=[];
    for ( prop in item) {
    if ( 'function' != typeof item[prop]) {
    msg.push(prop + ' : ' + item[prop]);
    }
    }
    alert(msg.join('\n'));
    }

    The 'typeof' test stops the method being printed out, comment it out
    to see everything. To see what's in the object, call the method:

    item.showContent();

    --
    Rob
     
    RobG, May 7, 2005
    #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. avnrao
    Replies:
    0
    Views:
    3,397
    avnrao
    May 7, 2004
  2. =?Utf-8?B?VGVyb3M=?=

    Dynamic Variables? OR Dynamic Controls

    =?Utf-8?B?VGVyb3M=?=, Aug 10, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    435
    Scott Allen
    Aug 10, 2004
  3. Replies:
    1
    Views:
    964
  4. dwok
    Replies:
    7
    Views:
    728
    Hal Rosser
    Mar 4, 2005
  5. Replies:
    9
    Views:
    1,006
Loading...

Share This Page