How to reference variable through function argument?

T

Tuxedo

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.
 
P

purcaholic

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
 
L

Laurent vilday

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";
 
R

Randy Webb

purcaholic said the following on 6/9/2007 4:15 AM:
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.
 
T

Tuxedo

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!
 
D

Douglas Crockford

Tuxedo said:
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/
 
P

purcaholic

purcaholic said the following on 6/9/2007 4:15 AM:




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
 
T

Tuxedo

purcaholic wrote:

[...]
JavaScript, apologize my ignorance.

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top