472,986 Members | 2,890 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,986 software developers and data experts.

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 2395
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
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
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
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
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
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
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
by: Kuku | last post by:
What is the difference between a reference and a pointer?
5
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
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
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
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
3
SueHopson
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...

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.