+ 1
Is it possible to do this?
Is it possible to do this in order to use a different variable name to set the same value everytime i call a function? var x; function test(){ x++; var "box"+x=something; alert("box"+x); }
4 Answers
+ 4
Yes, you can use as an object.
Stop. Reorganise your code. If you want to select variables with a variable, then there has to be a logical grouping for them. Make it explicit.
var id = 3;
var chat =
{
"1": "one",
"2": "two"
};
function variable_generator(value)
{
chat[id++] = value;
}
Add a new data will like this
variable_generator("data");
and than test your variable.
alert(chat[3]);
+ 1
You can use the fact that in JS, objects can be called as arrays and do that using an object.
var object = {};
var x = 0;
function test() {
x++;
object["box" + x] = something;
alert("box" + x);
}
Of course, it won't be directly the variable name, but I don't think you can do as you wanted.
0
Y U NO CODE