Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > HTML > Help with Javascript: naming variables using other variables

Reply
Thread Tools

Help with Javascript: naming variables using other variables

 
 
sagejoshua
Guest
Posts: n/a
 
      11-06-2005
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
 
Reply With Quote
 
 
 
 
Jonathan N. Little
Guest
Posts: n/a
 
      11-06-2005
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


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


--
Take care,

Jonathan
-------------------
LITTLE WORKS STUDIO
http://www.LittleWorksStudio.com
 
Reply With Quote
 
 
 
 
sagejoshua
Guest
Posts: n/a
 
      11-06-2005
On Sun, 06 Nov 2005 16:54:43 GMT, "Jonathan N. Little"
<(E-Mail Removed)> wrote:

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


Awesome. Works like a charm. Thanks for the quick help.

Josh
 
Reply With Quote
 
Michael Winter
Guest
Posts: n/a
 
      11-06-2005
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.
 
Reply With Quote
 
X l e c t r i c
Guest
Posts: n/a
 
      11-06-2005
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.");

Later, Art.

 
Reply With Quote
 
Jonathan N. Little
Guest
Posts: n/a
 
      11-06-2005
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
 
Reply With Quote
 
X l e c t r i c
Guest
Posts: n/a
 
      11-07-2005
Jonathan*N.*Little wrote:

"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"

Actually my point was about the unnecessary use of eval.

Michael and the other experts at comp.lang.javascript have helped me
many times, cwdjr too. And even though in this case his reply was more
directly related to and appropriate for the OP's question, who is to say
what is "far better". As we don't know the context with which this bit
of code will be used.

I could easily say that this:

var hither = "Close by";
var yonder = "In the hills!";
alert(hither);
alert(yonder);

is far more efficient and practical than what you offer, as the results
will be the same with a more simplistic approach. What I can't say
though is that this is the solution in any given situation without
knowing the application.

Later, Art.

 
Reply With Quote
 
Jonathan N. Little
Guest
Posts: n/a
 
      11-07-2005
X l e c t r i c wrote:

> Actually my point was about the unnecessary use of eval.
>
> Michael and the other experts at comp.lang.javascript have helped me
> many times, cwdjr too. And even though in this case his reply was more
> directly related to and appropriate for the OP's question, who is to say
> what is "far better". As we don't know the context with which this bit
> of code will be used.
>
> I could easily say that this:
>
> var hither = "Close by";
> var yonder = "In the hills!";
> alert(hither);
> alert(yonder);
>
> is far more efficient and practical than what you offer, as the results
> will be the same with a more simplistic approach. What I can't say
> though is that this is the solution in any given situation without
> knowing the application.


I agree accessing variables directly is simpler and more efficient, but
the OP was looking for a JS version of a PHP's variable variable which
the above method does not address. Personally I avoid variable
variables, I find you can really get yourself into trouble and they can
be a b*tch to debug, but again it was what the OP asked.

--
Take care,

Jonathan
-------------------
LITTLE WORKS STUDIO
http://www.LittleWorksStudio.com
 
Reply With Quote
 
sagejoshua
Guest
Posts: n/a
 
      11-07-2005
On Sun, 6 Nov 2005 11:54:41 -0600, http://www.velocityreviews.com/forums/(E-Mail Removed) (X l e c t r i
c) wrote:

><?php
>$var1 = "foo";
>${$var1.bar} = "great!";
>print $foobar;
>// Outputs "great!"
>?>



>Are you sure that outputs "great!" ?



Yep.

Josh
 
Reply With Quote
 
X l e c t r i c
Guest
Posts: n/a
 
      11-07-2005
Jonathan*N.*Little wrote:

"but again it was what the OP asked."

Which in fact is the bottom line. And regardless of your opinions you
offered a solution. Which is the way it should be.

I hope you do the same when I ask for help.

Later, Art.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
while executing my client program i get the exception javax.naming.LinkException: [Root exception is javax.naming.LinkException: [Root exception is javax.naming.NameNotFoundException: remaining if plz anybody know how to solve this problem then mahesh Java 0 03-08-2007 12:26 PM
Javax.naming Exception: name not found in naming service. Harman Java 1 07-28-2006 08:51 AM
naming variables in code helena Java 6 01-24-2005 04:12 PM
Naming of ampersands and other tricky characters Andy Dingley XML 1 06-10-2004 03:34 PM
xsl naming variables sb1 XML 2 05-07-2004 12:31 PM



Advertisments