Forum Moderators: open

Message Too Old, No Replies

Naming a variable in a function...

         

benj0323

2:49 am on Oct 10, 2004 (gmt 0)

10+ Year Member



This might seem kind of nub, but I cant seem to figure this out. I want to name a variable in a function using a parameter of that function.

For example:

function some_func(number)
{
var testVariable+number = 5;
}

So lets say the "number" parameter was passed the value 2. I would want the variable "testVariable2" to equal 5. Can I do this or do I have to make an array like:

testVariable[number]

Thanks a lot for your help.

upside

4:36 am on Oct 10, 2004 (gmt 0)

10+ Year Member



I believe that the only way to accomplish this is with an array as you suspected. Any reason why you wanted to avoid using an array?

Rambo Tribble

5:41 am on Oct 10, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I don't believe you can implement string substitution in a variable name other than through an array, but since global variables are kept in an array under the window object, it is possible to create a global variable using string substitution through associative array syntax as follows. What it might gain you over a local array, either indexed or associative, is unclear, unless you want to create a global.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Untitled</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
function varTest(x){
window[x]="a";
alert(b);
}
</script>
</head>
<body onload="varTest('b');">
</body>
</html>

Bernard Marx

9:12 am on Oct 10, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



benj0323, good morning.

How about giving us a sample of the kind of function you're working on? That way, we could could work out the most relevant approach(es).

Rambo Tribble

4:04 pm on Oct 10, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Actually, it occurs to me that you could create a local variable using associative array syntax, as the following script demonstrates. You would have to (in the example) always refer to the variable as q.variable_name, but the variable would only have local scope. Again, I'm not sure it isn't more trouble than it's worth.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Untitled</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
function varTest(x,y){
window[x]="a";

var q=window.varTest;
q[y]="c";
alert(q.d);
}
</script>
</head>
<body onload="varTest('b','d');">
<a href="#" onclick="alert(b);">test global</a><br />
<a href="#" onclick="alert(q.d);">test q.local</a><br />
</body>
</html>

benj0323

9:04 pm on Oct 10, 2004 (gmt 0)

10+ Year Member



Actually I solved the problem on my own. The reason I didnt want to use an array was because using an array didnt work correctly. Here is the code I was using:

var wysiwygEditor = new Array();
wysiwygEditor1 = new FCKeditor( arguments[i] ) ;
wysiwygEditor1.BasePath = 'wysiwyg/' ;
if (! document.all )
wysiwygEditor1.Config['EnableXHTML'] = false ;
wysiwygEditor1.ReplaceTextarea() ;

where "1" was the unique part of the variable. Using an array there didn't work, because there was a bug in the WYSIWYG editor. It's all fixed now and Im using this code:

var wysiwygEditor = new Array();
wysiwygEditor[i] = new FCKeditor( arguments[i] ) ;
wysiwygEditor[i].BasePath = 'wysiwyg/' ;
if (! document.all )
wysiwygEditor[i].Config['EnableXHTML'] = false ;
wysiwygEditor[i].ReplaceTextarea() ;

Thanks a lot for everyone's help.

Bernard Marx

10:31 pm on Oct 10, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I need a new FCKeditor.
Does anybody know where I can get one?

Rambo Tribble

1:11 am on Oct 11, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You could try the site, [fckeditor.net...] , or SourceForge, [sourceforge.net...]

I believe the production version is 1.6 and version 2 is in its second beta.