|
Posted by Jonathan N. Little on 11/06/05 21:20
X l e c t r i c wrote:
> sagejoshua wrote:
>
> "Help!
>
> Is there a way to declare a variable name using other variables in
> Javascript? In PHP, it's done with something like:
>
> <?php
> $var1 = "foo";
> ${$var1.bar} = "great!";
> print $foobar;
> // Outputs "great!"
> ?>
>
> Is there a similar method in Javascript?
>
> Thanks.
>
> Josh"
>
> Are you sure that outputs "great!" ?
>
> Jonathan N. Little wrote:
>
> "var foo="Value of Foo";
> var bar="foo";
> alert( "foo=" + foo + "\n evail of bar=" + eval(bar) );"
>
> There's no need for eval (or evail):
>
> var foo = "Value of Foo";
> var bar = foo;
> alert("foo = " + foo + ".\n\nBecause the value of the variable bar now
> equals the value of the variable foo, bar also = " + bar + ", without
> the need of eval.");
You misunderstand, OP wants a variable-variable not a copy of a variable
with a different identifier, that is a variable with another variable's
name in it.
var hither="Close by";
var yonder="In the hills!";
Michael has the far better solution to avoid 'evail'
var global = this;
var whereAmI="hither";
alert(global[whereAmI]); //displays 'Close by'
whereAmI="yonder";
alert(global[whereAmI]); //now displays 'In the hills!'
--
Take care,
Jonathan
-------------------
LITTLE WORKS STUDIO
http://www.LittleWorksStudio.com
[Back to original message]
|