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.
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.
Share Improve this question edited Oct 14, 2010 at 16:42 Elliott asked Oct 14, 2010 at 15:59 ElliottElliott 3,86424 gold badges71 silver badges93 bronze badges 10- In this particular case, that's a stupid idea. There is no point in dynamically changing the name of a single local variable. Just use a generic name. However, there are other cases (involving multiple variables) where this is a very powerful technique. – SLaks Commented Oct 14, 2010 at 16:10
- 2 Note that you can't begin a variable name with a numeric character in Javascript. – Robusto Commented Oct 14, 2010 at 16:14
- 1 Am not trying to rename a variable, am trying to use the value of a variable to create a new variable with the value as the name. – Elliott Commented Oct 14, 2010 at 16:15
- @Robusto I wouldnt begin with a number, this is just an example. – Elliott Commented Oct 14, 2010 at 16:15
- 1 The "word" is just any word, in the example the word is something. Sorry for the confusion its hard to explain. Make a new varaible and set the name of the new variable from the value of another varaible. – Elliott Commented Oct 14, 2010 at 16:39
8 Answers
Reset to default 5Everything 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";
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 };
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
}
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";
}
If you're using numeric "variables" like in your exemple (var 1; var 2;) you should use Array;
var list=[]; //declaration
list[1]="something"; //create a "variable" 1 with 'something' inside
list[2]="other thing"; //create another variable
var name=3;
var value="Hello";
list[name]=value; //works too
alert( list[1] ); //something
if you're using words as "keywords" you should use objects instead of Arrays
var list={}; //declaration
list["car1"]="Honda";
list["car2"]="Toyota";
list["fuit"]="Apple";
var name="greeting";
var value="Hello";
list[name]=value; //works too
alert( list["car2"] ); //Toyota
hope you understood.
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";
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?
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]