I am new to the javascript object model (prototype based) and am trying to do the following:
I have a constructor that simply initializes an array: this.myArray = []
. I have a method:
MyConstructor.prototype.addToArray = function(myObject, type) {
//
};
Basically I need myArray
to be attached to a specific type
. I.e. when addToArray()
is called, the object will be added to an array associated with the type
. I don't want to have to know all the possible types ahead of time. I also will need to add methods that clear the array holding objects of a certain type. So basically, I think I need to somehow create arrays dynamically which are associated with a type.
Any help would be appreciated.
I think my question is confusing so I'll try to elaborate: my "business" code creates objects which I need to keep track of. Each objects is associated with a certain "type" or "flavor". I am trying to make a generic object which handles storing these object references in arrays (an array per type) and handling operations on these objects. Operations then could be performed on all objects of a given type. I want to be able to do this without knowing the types ahead of time (i.e. avoid creating 1 array per type in the constructor).
"Type" could be anything. i.e. a string "typeA", or "typeB" etc. just a way of differentating between different classes of objects.