Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4554522
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T17:05:55+00:00 2026-05-21T17:05:55+00:00

I apologize because this topic comes up a lot, but I have not been

  • 0

I apologize because this topic comes up a lot, but I have not been able to have this adequately explained in anything I’ve read today.

I am trying to make a simple collection class (and learn about javascript prototyping at the same time) designed to store objects with a “name” property and lets its members be accessed by index or value. So far I’ve got this:

function Collection() {}
Collection.prototype.constructor = Collection;
Collection.prototype._innerList = [];
Collection.prototype._xref = {};
Collection.prototype.count = function () { return this._innerList.length; };
Collection.prototype.add = function (obj) {
    this._xref[obj.name] = this._innerList.push(obj) - 1;
}
Collection.prototype.get = function (id) {
    if (typeof id == "string") {
        return this._innerList[this._xref[id]];
    } else {
        return this._innerList[id];
    }
};

http://jsfiddle.net/YrVFZ/

The problem:

var foo = new Collection();
foo.add({name: "someitem", value:"hello world"});   // foo.count()== 1

var bar= new Collection();
bar.add({name: "someotheritem", value:"hello world"}); // bar.count()== 2

Hmm…

Basically, the new instance bar is created with all the properties having the current values of the data in foo. I know I can fix this by putting _xref, etc. inside the constructor, but I’m trying to understand how prototyping works. If I create a new instance, and make changes to the data in that instance, why would those values carry over when I create another new instance?

If I make further changes to a property from the prototype of foo or bar they are independent, so it doesn’t seem as if I’m somehow referencing the same instance of anything. So what is causing bar to be instantiated with the current values from foo?

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T17:05:56+00:00Added an answer on May 21, 2026 at 5:05 pm

    Consider a classroom full of students. Putting something on the prototype is like putting something on the white board for them all to see. When you’re declaring

    Collection.prototype._innerList = [];
    

    you’re giving every collection that property; regardless of calling new Collection() any changes to the white board affects all students. However, if you define it within the constructor, or one of the functions as this.variableName = [], each copy will have its own variableName, like handing each student a handout. Obviously, there’s some cases when it’s okay to have something on the white board, such as instructions that will be universal from student to student, but if each item is going to be different for each student, it should be an individual property. Hope this explanation makes sense…

    You want to be doing this.

    function Collection() {
        if (!this instanceof Collection)
            return new Collection();
        this._innerList = [];
        this._xref = {};
    
        return this;
    }
    
    Collection.prototype.count = function() {
        return this._innerList.length;
    };
    Collection.prototype.add = function(obj) {
        this._xref[obj.name] = this._innerList.push(obj) - 1;
    }
    Collection.prototype.get = function(id) {
        if (typeof id == "string") {
            return this._innerList[this._xref[id]];
        } else {
            return this._innerList[id];
        }
    };
    
    var foo = new Collection();
    foo.add({name: "someitem", value:"hello world"});   
    console.log(foo.count()); // 1
    
    var bar= new Collection();
    bar.add({name: "someotheritem", value:"hello world"});
    console.log(bar.count()); // 1
    

    http://jsfiddle.net/vXbLL/

    Edit

    Not really relevant to your question, but it’s something I do so I will throw it out there. Whenever I’m doing something on the prototype, if I’m not returning something, I return this. It allows chaining, so you could do instance.function1().function2().function3() as long as function1 and function2 return this.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I apologize because I know this has been covered over and over again, but
I apologize if this has been asked before. I searched but did not find
I apologize in advance because this is somehow a silly question, but I just
I apologize if this has been asked before, but I haven't quite found the
I apologize if this question was asked already before but I could not find
I apologize if this was asked somewhere else, but I do not know how
I apologize in advance for this somewhat ignorant question, but I have researched this
I'd like to apologize in advance, because this is not a very good question.
First off I apologize... I have posted this question before, but I did a
I apologize if my question is simple, but I have done a lot of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.