0

I'm making a digital library with three classes: Library, Shelf & Book. Shelves have their contents as an array of books. Books have two methods, enshelf and unshelf. When a book gets unshelfed it's supposed to set delete the instance of itself from the shelf it's on and then set it's location property to null. How can I modify the shelf it's sitting on? In the constructor if I change this.location, it will just give that property a new value instead of modifying the variable it points to. I feel like this is really simple and I'm overlooking something super basic.

var _ = require('lodash');

//books
var oldMan = new Book("Old Man and the Sea", "Ernest Hemingway", 0684801221);
var grapes = new Book("The Grapes of Wrath", "John Steinbeck", 0241952476);
var diamondAge = new Book("The Diamond Age", "Neal Stephenson", 0324249248);

//shelves
var shelf0 = new Shelf(0);
var shelf1 = new Shelf(1);

//libraries
var myLibrary = new Library([shelf0, shelf1], "123 Fake Street");

//these need to accept an unlimited amount of each
function Library(shelves, address) {
    this.shelves = shelves; //shelves is an array
    this.address = address;
    this.getAllBooks = function() {
        console.log("Here are all the books in the library: ");
        for (var i = 0; i < this.shelves.length; i++) {
            console.log("Shelf number " + i + ": ");
            for (var j = 0; j < this.shelves[i].contents.length; j++) {
                console.log(this.shelves[i].contents[j].name);
            }
        }
    }
}

function Shelf(id) {
    this.id = id;
    this.contents = [];
}

function Book(name, author, isbn) {
    this.name = name;
    this.author = author;
    this.isbn = isbn;
    this.location = null;
    this.enshelf = function(newLocation) {
        this.location = newLocation;
        newLocation.contents.push(this);
    }
    this.unshelf = function() {
        _.without(this.location, this.name); //this doesn't work
        this.location = null;
    }
}


console.log("Welcome to Digital Library 0.1!");

oldMan.enshelf(shelf1);
myLibrary.getAllBooks();
oldMan.unshelf();
myLibrary.getAllBooks();
1
  • use [].splice() to modify, functional solutions like _.without() make copies. Commented Sep 20, 2014 at 23:52

2 Answers 2

1

Small issue with your unshelf method, easily remedied:

this.unshelf = function() {
    this.location.contents = 
        _.without(this.location.contents, this);
    this.location = null;
}

Consider, however, that shelf and unshelf should be methods of Shelf, and not of Book. Also, if you must have this method, surround it with a guard, like so:

this.unshelf = function() {
    if (this.location) {
      this.location.contents = 
          _.without(this.location.contents, this);
      this.location = null;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

Couple of small issues:

without works on arrays and returns a copy of the array with the elements removed - the original is untouched. So you need to pass location.contents instead of just location and reassign it back to location.contents.

Also you add the whole book to the Shelf, then try to remove it by name, so it doesn't match and get removed. So just pass this to without:

this.unshelf = function() {
    if (this.location) {
        this.location.contents = _.without(this.location.contents, this);
        this.location = null;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.