I want to be able to create and unknown number of objects. I'm not sure if there is a better way to manage and reference them.
Lets use a standard OOP example... say every time a user enters a name for a pet in a text field and clicks a button a new pet object is created via the petFactory function.
function pet(name)
{
this.name= name;
}
function petFactory(textFieldInput)
{
var x = new pet(textFieldInput)
}
Since there's no way, that I know of (besides an eval function), to dynamically use a new, unique variable name for the pet every time petFactory is called, x is reassigned to a new object and the last pet is lost. What I've been doing is pushing the pet object onto an array as soon as it's initialized.
petArray.push(this);
So if I wanted the pet named 'mrFuzzyButtoms' I could loop through an indexed array until I found the object with the instance variable 'mrFuzzyButtoms'
for (n in petArray)
{
if (petarray[n].name == 'mrFuzzyBottoms')
{
// Kill mrFuzzyBottoms
}
}
or I could use an associative array...whatever...but the array is the only method I know for doing this. Besides using an eval function to create unique variable names from strings but some languages don't have an eval function (actionscript3) and then there is the security risk.
Is there a better way to do this?
Edit: Right now I'm working in Python, JavaScript and ActionScript.