473,511 Members | 16,983 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
8 2405
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

36
28052
by: Riccardo Rossi | last post by:
Hi all! How does Python pass arguments to a function? By value or by reference? Thanks, Riccardo Rossi.
4
3378
by: mangi03 | last post by:
Hi, I came acrosss g++ compile errors whenever I make a function call by reference and found out from the test program that compiler is treating the function argument differently when another...
7
1438
by: Richard Cavell | last post by:
Hi, The point of using const on a parameter to a function should be to let your compiler know that the parameter shouldn't be modified during your program. This allows you to keep your code...
110
9812
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
7
2937
by: Xiaoshen Li | last post by:
Dear All, I thought I understood using pointer variables as function parameters. But I failed to understand why it is needed pass-by-reference of a pointer variable. To me, pointer variable...
2
3035
by: Jake Barnes | last post by:
Using javascript closures to create singletons to ensure the survival of a reference to an HTML block when removeChild() may remove the last reference to the block and thus destory the block is...
51
4394
by: Kuku | last post by:
What is the difference between a reference and a pointer?
5
2300
by: josh | last post by:
Hi, how really work behind the scene the pass-by-reference in C++? I know that in C++ like the C the objects are passed by-values and if we want we can simulate the pass-by-reference using the...
10
13609
by: Robert Dailey | last post by:
Hi, I noticed in Python all function parameters seem to be passed by reference. This means that when I modify the value of a variable of a function, the value of the variable externally from the...
275
12038
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
7144
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7427
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5671
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5069
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4741
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3227
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3214
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1577
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
785
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.