I have created a class array (or variable that contains an array) and am trying to add an element to the array when a new Element object is created. Thus, the line of code to add a new element to the array is within the Element object’s constructor function. However, the browser just gives me an error “object expected on line 9”, the line of code trying to add an element to the array.
Are arrays not functional within objects? Or, is it that the array is made as a class variable?
[CODE]function Element() {
// INSTANCE VARIABLES
this.name = “newElement” + Element.total_elements_created; // GIVE UNIQUE NAME
// INITIALIZATIONS
Element.element_count ++; // INCREASE TOTAL NUMBER OF ELEMENTS EXISTING
Element.total_elements_created ++; // INCREASE NUMBER OF ELEMENTS EVER CREATED
Element.array_element_names[Element.element_count] = this.name; // PLACE NAME INTO REFERENCE ARRAY
}
// FORCE PROTOTYPE OBJECT TO BE CREATED
new Element();
// CLASS VARIABLES
Element.element_count = 0; // TOTAL NUMBER OF EXISTING ELEMENTS
Element.total_elements_created = 0; // USED TO HELP GIVE UNIQUE NAME TO CREATED ELEMENTS
Element.array_element_names = new Array(); // LIST OF NAMES OF EACH ELEMENT
// METHODS
Element.prototype.Element_Count = function() { return Element.element_count; } // GET TOTAL NUMBER OF EXISTING ELEMENTS
// DESTRUCTOR
Element.prototype.Die = function() {
// DECREASE TOTAL EXISTING ELEMENT COUNT
Element.element_count –;
// REMOVE ELEMENT NAME FROM LIST
// CLEAR ELEMENT’S PROPERTIES
for(var i in this) {
this[i] = null;
}
}