Reply to Re: Help with Javascript: naming variables using other variables

Your name:

Reply:


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]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация