467,894 Members | 1,533 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,894 developers. It's quick & easy.

How to reference variable through function argument?

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.
Jun 9 '07 #1
  • viewed: 2253
Share:
8 Replies
On 9 Jun., 08:26, Tuxedo <tux...@mailinator.netwrote:
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

Jun 9 '07 #2
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
Jun 9 '07 #3
purcaholic said the following on 6/9/2007 4:15 AM:
On 9 Jun., 08:26, Tuxedo <tux...@mailinator.netwrote:
>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/
Jun 9 '07 #4
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!
Jun 9 '07 #5
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/
Jun 9 '07 #6
On 9 Jun., 12:47, Randy Webb <HikksNotAtH...@aol.comwrote:
purcaholic said the following on 6/9/2007 4:15 AM:


On 9 Jun., 08:26, Tuxedo <tux...@mailinator.netwrote:
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

Jun 9 '07 #7
purcaholic wrote:

[...]
JavaScript, apologize my ignorance.
No need - I will dwelve into those subjects at some stage and so the
information is only helpful.

Jun 9 '07 #8
Douglas Crockford wrote:

[..]
Objects are good.
Thanks for this example. I'm trying to convert into the object form habit.

Jun 9 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

36 posts views Thread by Riccardo Rossi | last post: by
7 posts views Thread by Richard Cavell | last post: by
7 posts views Thread by Xiaoshen Li | last post: by
51 posts views Thread by Kuku | last post: by
5 posts views Thread by josh | last post: by
10 posts views Thread by Robert Dailey | last post: by
275 posts views Thread by Astley Le Jasper | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.