I'm having trouble figuring out how to approach this issue.
Currently, I have a variable set up to store an object:
var myobject = new object();
Everything is working fine; the name of the variable is 'my object' and it contains a new object.
If I need 10 variables, I would have to manually create 10 variable names like so:
var myobject1 = new object();
var myobject2 = new object();
var myobject3 = new object();
var myobject4 = new object();
... and so on
Now, I want to automate the process by incrementing the name of the variable (not the content) using a counter. This way, regardless of how many new objects I need, the name will change in an incremental fashion. Unfortunately, JavaScript doesn't seem to support this directly.
I've tried to come up with a logical solution, but I'm struggling with how to actually increment the variable name:
//global variables
var counter=0;
var tempname;
function makeplentyofobj(){
if (temp name)
tempname= new object(); //this should make myobjectN
else
myobject= new object(); //this makes myobject1
counter ++;
tempname ='myobject'+counter; //this makes a name for the next object)
}
When I run this code, the compiler assigns the new object to myobject, increments the counter to 1, and stores the string 'myobject1' in tempname. If I call the function a second time, the counter is now 1 and tempname holds 'myobject1'. This is the desired name for the next variable. However, since tempname is not empty, the else condition is triggered, leading to the conversion of its content from a string to an object.
Instead, what I truly want is to generate a new variable name that increments with each new object creation, such as:
myobject1=new object();
myobject2= new object();
...
Is there a way to achieve this in JavaScript?
EDIT:
Thank you for the response; I understand that this question may be similar to others, but I put effort into explaining my thought process. The next time, I'll try to simplify it more.