/    Sign up×
Community /Pin to ProfileBookmark

Class array in custom object not working

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;
}
}[/CODE]

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@toicontienJan 20.2010 — [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.push( 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;
}
}[/CODE]
Copy linkTweet thisAlerts:
@chris_m_mauthorJan 22.2010 — Toicanien,

Array.push();

Such a simple solution. Why didn't I think of that? Thank you very much.

  • * EDIT *


  • Actually, now I am getting the error: "Element.array_element_names is null or not an object".
    Copy linkTweet thisAlerts:
    @toicontienJan 23.2010 — Oh my gosh. I can't believe I didn't see this before. You are instantiating a new Element object before you are declaring the class level variables. Move the "new Element()" line below "Element.array_element_names = new Array();"
    Copy linkTweet thisAlerts:
    @chris_m_mauthorJan 25.2010 — toicontien,

    That makes sense. Works like a charm now! Thank you for your help again. ?
    ×

    Success!

    Help @chris_m_m spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 6.16,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @nearjob,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,
    )...