JavaScript How do I get each instance to inherit their own instance of a member?

In the following code, I have two Book Collections. One collection has "Moby Dick," while the other collection has "The Firm." However, when I look at the second collection, it also has Moby Dick. How do I create Books that do not share the same data?

function Collection () {
    var stuff = [];
    this.get = function () {
        return stuff;
    };
    this.add = function (item) {
        stuff.push(item);
    };
}
function Books () {}
Books.prototype = new Collection ();

var myBooks = new Books();
myBooks.add("Moby Dick");

var yourBooks = new Books();
yourBooks.add("The Firm");

console.log(yourBooks.get()); // ["Moby Dick", "The Firm"]

I ended up going with this:

function Collection () {
    var stuff = [];
    this.get = function(){
        return stuff;
    };
    this.add = function(item){
        stuff.push(item);
    };
}
function Books () {
    Collection.call(this);
};

Books.prototype = new Collection();

Books.prototype.constructor = Books;

var myBooks = new Books();
myBooks.add("Moby Dick");

var yourBooks = new Books();
yourBooks.add("The Firm");

console.log(myBooks.get());
Answer:1

Use a per-instance value with this:

function Books () {
    this.stuff = [];
}

And then in the collection:

this.get = function () {
    return this.stuff;
};

Currently only one stuff array is created when you set the prototype of Books. All the inherited getters and setters then refer to that one variable.


Complete code:

function Collection () {
    this.get = function () {
        return this.stuff;
    };
    this.add = function (item) {
        this.stuff.push(item);
    };
}
function Books () {
    this.stuff = [];
}

Books.prototype = new Collection ();

var myBooks = new Books();
myBooks.add("Moby Dick");

var yourBooks = new Books();
yourBooks.add("The Firm");

console.log(myBooks.get()); // ["Moby Dick"]
console.log(yourBooks.get()); // ["The Firm"]
Answer:2

Check here javascript:Function("alert('this is ok')")(); same as new Function("alert('this is ok')")(); What the syntax called ? Any reference ? EDIT Sorry, my fault,it's only a label,and it's ...

Check here javascript:Function("alert('this is ok')")(); same as new Function("alert('this is ok')")(); What the syntax called ? Any reference ? EDIT Sorry, my fault,it's only a label,and it's ...

  1. what does javascript do
  2. what does javascript mean
  3. what does javascript look like
  4. what does javascript void mean
  5. what does javascript need to work on a website
  6. what does javascript compile to
  7. what does javascript stand for
  8. what does javascript void(0)
  9. what does javascript code look like
  10. what does javascript do for a website
  11. what does javascript do in html
  12. what does javascript run on
  13. what does javascript error mean
  14. what does javascript eval do
  15. what does javascript map return
  16. what does javascript developer do
  17. what does javascript require do
  18. what does javascript trim do
  19. what does javascript await do
  20. what does javascript have to do with java

I am designing a game in HTML5 with Canvas/JavaScript. My aim is to create a timer that will countdown from a certain amount of minutes down to zero, during the time you need to shoot as many ...

I am designing a game in HTML5 with Canvas/JavaScript. My aim is to create a timer that will countdown from a certain amount of minutes down to zero, during the time you need to shoot as many ...

  1. create timer javascript
  2. creating timer js
  3. create countdown timer javascript
  4. create timer using javascript
  5. create simple timer javascript
  6. create timer event javascript

Hello guys I have some problem with my website. Here's the situation: Page 1 <script type="text/javascript" src="jscript.js"> <input type="button" value="change" onClick="changeAttr()"> ...

Hello guys I have some problem with my website. Here's the situation: Page 1 <script type="text/javascript" src="jscript.js"> <input type="button" value="change" onClick="changeAttr()"> ...

  1. change element attribute javascript
  2. change element attribute jquery
  3. change element attribute microstation
  4. change element attribute selenium
  5. change element attribute value jquery
  6. change element attribute
  7. change element attribute css
  8. change element attribute angular
  9. change element attribute angularjs
  10. change element attribute selenium python
  11. change element attribute html
  12. js change element attribute
  13. change html element attribute javascript
  14. vue change element attribute
  15. react change element attribute
  16. js change element attribute value
  17. puppeteer change element attribute
  18. vuejs change element attribute
  19. protractor change element attribute
  20. change dom element attribute

In our frontend build scheme we need to resize images for srcset attribute. I've tried to use gulp-responsive and other Gulp and Grunt plugins, but quality of result images is unacceptable. So, can I ...

In our frontend build scheme we need to resize images for srcset attribute. I've tried to use gulp-responsive and other Gulp and Grunt plugins, but quality of result images is unacceptable. So, can I ...