Jump to content
  • Advertisement
Sign in to follow this  
ENEX

[web] Javascript: Variable's Variable

This topic is 4838 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have no idea on how to put a variable in a variable's name, tried something like var1+var2 = 'something' and some other combinations. Please help!

Share this post


Link to post
Share on other sites
Advertisement
eval() might be what you're looking for - it allows you to execute a string as if it was a piece of JavaScript. For example:
var test_var = 'Hello'; // Our test variable
var var_name = 'test_var'; // Set var_name to equal the name of our test variable
eval(var_name+"='Goodbye'"); // "eval()" some code that changes the value in our test variable
alert(test_var);
displays a message box that says 'Goodbye' on it.

Share this post


Link to post
Share on other sites
Eval is pretty inefficient, though, so if you can find any other way to do what you want, use that instead. I'm not sure of the context of your code, but you could probably get by using an array (where the key is the part number).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!