Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > How to reference variable through function argument?

Reply
Thread Tools

How to reference variable through function argument?

 
 
Tuxedo
Guest
Posts: n/a
 
      06-09-2007
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.
 
Reply With Quote
 
 
 
 
purcaholic
Guest
Posts: n/a
 
      06-09-2007
On 9 Jun., 08:26, Tuxedo <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
 
Laurent vilday
Guest
Posts: n/a
 
      06-09-2007
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
 
Reply With Quote
 
Randy Webb
Guest
Posts: n/a
 
      06-09-2007
purcaholic said the following on 6/9/2007 4:15 AM:
> On 9 Jun., 08:26, Tuxedo <(E-Mail Removed)> 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/
 
Reply With Quote
 
Tuxedo
Guest
Posts: n/a
 
      06-09-2007
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!
 
Reply With Quote
 
Douglas Crockford
Guest
Posts: n/a
 
      06-09-2007
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/
 
Reply With Quote
 
purcaholic
Guest
Posts: n/a
 
      06-09-2007
On 9 Jun., 12:47, Randy Webb <(E-Mail Removed)> wrote:
> purcaholic said the following on 6/9/2007 4:15 AM:
>
>
>
>
>
> > On 9 Jun., 08:26, Tuxedo <(E-Mail Removed)> 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

 
Reply With Quote
 
Tuxedo
Guest
Posts: n/a
 
      06-09-2007
purcaholic wrote:

[...]

> JavaScript, apologize my ignorance.


No need - I will dwelve into those subjects at some stage and so the
information is only helpful.

 
Reply With Quote
 
Tuxedo
Guest
Posts: n/a
 
      06-09-2007
Douglas Crockford wrote:

[..]

> Objects are good.


Thanks for this example. I'm trying to convert into the object form habit.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Variable argument function as a parameter of a variable argument function AikidoGuy C Programming 11 11-21-2011 10:43 PM
Reference assignment through base class reference siddhu C++ 12 06-06-2007 03:32 PM
A dllimport variable is accessed indirectly through a variable hsharsha@gmail.com C++ 2 02-11-2006 02:26 PM
Looping through variable number of arrays variable times? Mike P Javascript 27 11-06-2005 10:16 AM



Advertisments