2

I am getting an error when I invoke a prototype function inside of a loop.

The class

var Book = function(title, Available, publicationDate, checkoutDate, callNumber, Authors) {
    this.title = title;
    this.Available = false;
    this.publicationDate = new Date();
    this.checkoutDate = checkoutDate;
    this.callNumber = 690080;
    this.Authors = Authors;
};

Other class with has the booksOut array property

var Patron = function(firstName, lastName, libCardNum, booksOut, fine) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.libCardNum = libCardNum;
    this.booksOut = [];
    this.fine = 0.00;
};

The prototype which is supposed to add the book to the array property in the patron class

Patron.prototype.read = function(Book) {
    this.booksOut.add(Book);
}

The loop that cause the TypeError: catalog[k].read is not a function error with the parenthesis and without them it always gives the same output.

for (var i = 0; i < 90; i++) {
    for (var j = 0; j < catalog.length; j++) {
        for (var k = 0; k < patrons.length; k++) {
            var fine = patrons[k].fine;
            if (catalog[k].Available) {
                catalog[k].checkOut();
            } else {
                catalog[k].checkIn();
                catalog[k].read();
                if (catalog[k].isOverdue()) {
                    fine = fine + 5.00;
                }
            }
            patrons[k].fine = fine;
        }
    }
}

Any help would be appreciated.

2
  • 3
    What is catalog? It it is an instance of Book then it does not have the read method, which is defined for Patron, not Book. Also, it seems to expect an argument.... Calling that parameter Book is not the best choice, as it can be misunderstood as being the Book constructor. Use init-caps only for constructors/classes. Commented Apr 23, 2017 at 19:38
  • Figured it out, appreciate the help. Commented Apr 23, 2017 at 20:27

1 Answer 1

1

The problem is that in your question you haven't stated clearly what catalog[k] is...
But I'm assuming it is of type Book, because it has properties like Available.
Then the error makes sense because you didn't define any method read for Book, you just defined it for Patron.

Therefore you could call the read function on Patron like this

...  
catalog[k].checkIn();  
patrons[k].read(catalog[k]);  
...
Sign up to request clarification or add additional context in comments.

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.