How to reference variable through function argument?

Discussion in 'Javascript' started by Tuxedo, Jun 9, 2007.

  1. Tuxedo

    Tuxedo Guest

    How can I modify any one of these global variable identified via a function
    argument?

    var x1 = "bla";
    var x2 = "bla";
    var x3 = "bla";

    function modify(variable) {

    ???? [variable]???? = "blabla":

    }

    The following onclick should change the string value of variable 'x1' from
    "bla" to "blabla", via the above modify() function.

    <a ... onclick="modify('x1')">

    Thanks in advance for any aswer to this simple question.
     
    Tuxedo, Jun 9, 2007
    #1
    1. Advertising

  2. Tuxedo

    purcaholic Guest

    On 9 Jun., 08:26, Tuxedo <> wrote:
    > How can I modify any one of these global variable identified via a function
    > argument?
    >
    > var x1 = "bla";
    > var x2 = "bla";
    > var x3 = "bla";
    >
    > function modify(variable) {
    >
    > ???? [variable]???? = "blabla":
    >
    > }
    >
    > The following onclick should change the string value of variable 'x1' from
    > "bla" to "blabla", via the above modify() function.
    >
    > <a ... onclick="modify('x1')">
    >
    > Thanks in advance for any aswer to this simple question.


    You can use eval() to evaluate a string like eval(variable + ' =
    "blabla";'); inside modify() method. This would update a value of an
    existing global javascript variable or creates a new variable. But
    eval should be used carefully, because it interpets each string
    containing valid javascript code.

    Better is usage of an a switch if/else if, scanning passed parameter
    and setting corresponding global variable or usage of an global
    assoziative array and passing the array key to the function.


    purcaholic
     
    purcaholic, Jun 9, 2007
    #2
    1. Advertising

  3. Tuxedo a écrit :
    > How can I modify any one of these global variable identified via a function
    > argument?
    >
    > var x1 = "bla";
    > function modify(variable) {
    > ???? [variable]???? = "blabla":


    window[variable] = "blabla";

    > }
    >
    > <a ... onclick="modify('x1')">


    --
    laurent
     
    Laurent vilday, Jun 9, 2007
    #3
  4. Tuxedo

    Randy Webb Guest

    purcaholic said the following on 6/9/2007 4:15 AM:
    > On 9 Jun., 08:26, Tuxedo <> wrote:
    >> How can I modify any one of these global variable identified via a function
    >> argument?
    >>
    >> var x1 = "bla";
    >> var x2 = "bla";
    >> var x3 = "bla";
    >>
    >> function modify(variable) {
    >>
    >> ???? [variable]???? = "blabla":
    >>
    >> }
    >>
    >> The following onclick should change the string value of variable 'x1' from
    >> "bla" to "blabla", via the above modify() function.
    >>
    >> <a ... onclick="modify('x1')">
    >>
    >> Thanks in advance for any aswer to this simple question.

    >
    > You can use eval() to evaluate a string like eval(variable + ' =
    > "blabla";'); inside modify() method.


    You can use a sledgehammer to drive a tack in the wall also. Does that
    mean you do it though?

    window[variable] = "blabla";
    alert('Look Ma, no eval and no if/else switch crap!!!')

    > Better is usage of an a switch if/else if, scanning passed parameter
    > and setting corresponding global variable or usage of an global
    > assoziative array and passing the array key to the function.


    Can you explain how to create an "associative array" in javascript?
    Beware, it is a loaded question.

    Besides, why do all that trouble when it is quite simple? See above.

    P.S. It has been a bad night so excuse my tone, I am just too tired to
    give a crap right now.

    --
    Randy
    Chance Favors The Prepared Mind
    comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
    Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
     
    Randy Webb, Jun 9, 2007
    #4
  5. Tuxedo

    Tuxedo Guest

    Randy Webb wrote:

    [...]

    > P.S. It has been a bad night so excuse my tone, I am just too tired to
    > give a crap right now.


    window[variable] is obviously the better solution for the given situation,
    while eval or switch may have been posted as other possible methods beyond
    the original question.

    Thanks for cutting the crap - I wish you a speedy recovery from last
    night's ordeal!
     
    Tuxedo, Jun 9, 2007
    #5
  6. Tuxedo wrote:
    > How can I modify any one of these global variable identified via a function
    > argument?
    >
    > var x1 = "bla";
    > var x2 = "bla";
    > var x3 = "bla";
    >
    > function modify(variable) {
    >
    > ???? [variable]???? = "blabla":
    >
    > }
    >
    > The following onclick should change the string value of variable 'x1' from
    > "bla" to "blabla", via the above modify() function.
    >
    > <a ... onclick="modify('x1')">


    It is generally a bad idea to rely on global variables. What you want can easily
    be done with an object.

    var xx = {
    x1: "bla",
    x2: "bla",
    x3: "bla"
    };

    function modify(variable) {
    xx[variable] = "blabla";
    }

    modify('x1');

    Objects are good.

    http://javascript.crockford.com/
     
    Douglas Crockford, Jun 9, 2007
    #6
  7. Tuxedo

    purcaholic Guest

    On 9 Jun., 12:47, Randy Webb <> wrote:
    > purcaholic said the following on 6/9/2007 4:15 AM:
    >
    >
    >
    >
    >
    > > On 9 Jun., 08:26, Tuxedo <> wrote:
    > >> How can I modify any one of these global variable identified via a function
    > >> argument?

    >
    > >> var x1 = "bla";
    > >> var x2 = "bla";
    > >> var x3 = "bla";

    >
    > >> function modify(variable) {

    >
    > >> ???? [variable]???? = "blabla":

    >
    > >> }

    >
    > >> The following onclick should change the string value of variable 'x1' from
    > >> "bla" to "blabla", via the above modify() function.

    >
    > >> <a ... onclick="modify('x1')">

    >
    > >> Thanks in advance for any aswer to this simple question.

    >
    > > You can use eval() to evaluate a string like eval(variable + ' =
    > > "blabla";'); inside modify() method.

    >
    > You can use a sledgehammer to drive a tack in the wall also. Does that
    > mean you do it though?
    >
    > window[variable] = "blabla";
    > alert('Look Ma, no eval and no if/else switch crap!!!')
    >
    > > Better is usage of an a switch if/else if, scanning passed parameter
    > > and setting corresponding global variable or usage of an global
    > > assoziative array and passing the array key to the function.

    >
    > Can you explain how to create an "associative array" in javascript?
    > Beware, it is a loaded question.

    no comment ;-)

    > Besides, why do all that trouble when it is quite simple? See above.
    >
    > P.S. It has been a bad night so excuse my tone, I am just too tired to
    > give a crap right now.
    >
    > --
    > Randy
    > Chance Favors The Prepared Mind
    > comp.lang.javascript FAQ -http://jibbering.com/faq/index.html
    > Javascript Best Practices -http://www.JavascriptToolbox.com/bestpractices/- Zitierten Text ausblenden -
    >
    > - Zitierten Text anzeigen -


    Your'e right, window[foo] is better than sledge hammer solutions such
    as eval or conditional statements. I didn't knew this posssibility in
    JavaScript, apologize my ignorance.


    purcaholic
     
    purcaholic, Jun 9, 2007
    #7
  8. Tuxedo

    Tuxedo Guest

    purcaholic wrote:

    [...]

    > JavaScript, apologize my ignorance.


    No need - I will dwelve into those subjects at some stage and so the
    information is only helpful.
     
    Tuxedo, Jun 9, 2007
    #8
  9. Tuxedo

    Tuxedo Guest

    Douglas Crockford wrote:

    [..]

    > Objects are good.


    Thanks for this example. I'm trying to convert into the object form habit.
     
    Tuxedo, Jun 9, 2007
    #9
    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. Ben Kial
    Replies:
    1
    Views:
    677
    Eric Enright
    Nov 15, 2004
  2. S?ren Gammelmark
    Replies:
    1
    Views:
    1,929
    Eric Sosman
    Jan 7, 2005
  3. Reckoner
    Replies:
    11
    Views:
    717
    Steven D'Aprano
    Jan 19, 2009
  4. AikidoGuy
    Replies:
    11
    Views:
    571
    Seebs
    Nov 21, 2011
  5. User1014
    Replies:
    1
    Views:
    192
    Richard Cornford
    Nov 30, 2006
Loading...

Share This Page