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

Your name:

Reply:


Posted by Michael Winter on 11/06/05 19:31

On 06/11/2005 16:54, Jonathan N. Little wrote:

[snip]

> var foo="Value of Foo";
> var bar="foo";
> alert( "foo=" + foo + "\n evail of bar=" + eval(bar) );

The eval function is evil. It's use is rarely ever necessary.

ECMAScript provides bracket notation as an alternative to dot notation
property accessors. This form allows any expression, including function
calls and variables, which will be evaluated and type-converted to a
string. The result will then be used as the property name.

Global variables, such as foo and bar, above, are members of the global
object:

var global = this; /* The window identifier also refers
* to the global object in browsers.
*/

alert(global[bar]); /* 'Value of Foo' */

See Bracket Notation[1] in the c.l.javascript FAQ notes.

Mike


[1] <http://www.jibbering.com/faq/faq_notes/square_brackets.html>

--
Michael Winter
Prefix subject with [News] before replying by e-mail.

[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

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