Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Explanation: Adding Methods to the Library Class1:20 with Ashley Boucher
Now that we have some basic objects to play with, let's flesh them out by adding some methods. We'll start with methods to add books and patrons to the Library objects.
Instructions for Workspace
Your instructions are as follows:
1) In the Library.js file, add two methods to the Library class:
addPatron()receives one argument, a Patron object. This object (stored in the
patronvariable) should be added to the Library object's (stored in the
patronsproperty, which is an array.
addBook()receives one argument, a Book object. This object (stored in the
bookvariable) should be added to the Library object's
booksproperty, which is an array.
Welcome back, we've done some great work on our library app so far.
We've declared three classes, library, patron, and book, and
added their constructor methods.
Now it's time to start adding some other methods.
Our rough plan of attack included two methods on the library class,
addBook() and addPatron().
These methods are our way of entering a new book or
new patron into the library system.
These methods will take an actual book or patron object and add it to the array of
books and patrons stored in the library object's books and patrons properties.
Let's start with addBook().
It makes sense that the addBook() method should receive the book that's being added
as a parameter.
We can pass in an entire book object,
like the one we created earlier that's representing a Harry Potter book.
Then we just need to add the book object to the array stored in the library's books
property using a simple array method like push.
This can be a simple, one-line method.
addPatron() will work exactly the same way.
Open up the attached workspace to find an updated read me file with
If you'd like to continue working in your existing workspace,
check out the teacher's notes for the new directions.
Once you're done, or
if you need some help, check out the solution following this video.
Then we'll join back up to test out our new methods.
You need to sign up for Treehouse in order to download course files.Sign up