tags:

views:

79

answers:

7

Hi is it possible to set a var name from another variable?

Such as I have the var test with different numbers each time the function is called so I want the number to be a variable name e.g. var test contains ' 1 ' so the new variable would be var 1. I then want to add a word to this number "word" to make the new variable become var 1word.

var newNumber = 1;
var newNumber += 'word';
       ^ becomes ' 1 '
the new var name becomes ' 1word '

edit:

function getNumber(newNumber) //  Gets a number, for this example say its the number '1' 
    {

        var newNumber += "something";  //The new varaible will be named "1something" (something can be any word)

    }

Hope I haven't made it sound too confusing, thanks.

+2  A: 

You can put the variables in an object and index the object using string keys.

For example:

var myData = { };
myData.x1 = 42;
alert(myData["x" + 1]);

You can also initialize the object using object notation:

var myData = { key: 42 };
SLaks
A: 

I'm confused by your question but it seems like you want to change a variables name which cannot be done. You can create a new variable with the desired name and assign it whatever value you want.

My question is why would you want to do this?

Abe Miessler
I have a function which passes elementIDs. I want to create a new variable with the name of the value in the elementID.
Elliott
I think you might be better off using an array, an object or some sort of loop. Without seeing your code I can't say for sure though.
Abe Miessler
+1  A: 

We need more of an explanation of what you are trying to accomplish. This looks like a really bad idea. Most likely you really should just used a named index array. Eg.

var stuff = {}
stuff[1] = "one";
stuff["name"] = "david";
Plaudit Design - Web Design
Say am passing a number to a fucntion, this is then stored in a variable. The value of the "number" (passed to function) becomes the name of the new variable.
Elliott
I think, umm, Mr Design, was suggesting this this is an X-Y problem. You think that variable variables (Y) will solve a problem (X), but you don't say what X is. (Variable variables are, BTW, evil nightmares that are horrible to maintain that should be avoided).
David Dorward
There is probably a better way to do what that doesn't involve determining variable names on the fly. Can you post your code?
Abe Miessler
+1  A: 

Everything in Javascript is basically just an associative array. This means you can use array access notation and dot notation more or less interchangeably. Array access notation provides the feature you are looking for:

object["name"]="foo"

will set a name property on the given object.

If you want to create a global variable then use the window object

window["name"]="foo"

You can also use an expression or a variable in the square brackets

window[someVaribale]="foo";
window["name_" + someVariable]="foo";
Ollie Edwards
+1  A: 

There's a simple way to achieve this, if you store all your variables inside an object/array. Remember, in javascript, arrays are objects, objects are objects etc :p

var mystuff = {}; // new object literal

var numVariables = 10;

for (var i = numVariables - 1; i >= 0; i--){
    var newkey          = i + "_my_index";
    mystuff[newkey]     = "some stuff";
}
danp
A: 

You may be able to achieve this with the builtin method: eval( "your javascript code here" );

so maybe in your case it would be something like: eval( "var yourNewName = \"" + yourOldVarValue + "\""); then nullify your old variable.

But as the others express, I can't see why or in which case you would want to rename a variable (unless its for some weird hacking trickery lol).

[not tested, so this eval may not even work :S]

bigp
+1  A: 

Far as I can understand, you want to create a variable that's named after the contents of another variable.

Not sure if you can do this directly, but you can do it pretty painlessly using objects.

var obj = new Object();
obj['original'] = 'name';
obj[obj['original']] = 'another name';

The object now has this structure:

{
  'original': "name",
  'name': "another name" #variable named after original's contents
}
tta
Thanks that what I wanted , I havent used objects before could you give me a quick example how I would alert the new object/variable created?
Elliott
alert(obj['name']) or alert(obj[obj['original']])
tta
Also, go here for docs: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object
tta