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.
[MUSIC] 0:00 Welcome back, we've done some great work on our library app so far. 0:04 We've declared three classes, library, patron, and book, and 0:08 added their constructor methods. 0:11 Now it's time to start adding some other methods. 0:13 Our rough plan of attack included two methods on the library class, 0:16 addBook() and addPatron(). 0:19 These methods are our way of entering a new book or 0:21 new patron into the library system. 0:24 These methods will take an actual book or patron object and add it to the array of 0:27 books and patrons stored in the library object's books and patrons properties. 0:31 Let's start with addBook(). 0:37 It makes sense that the addBook() method should receive the book that's being added 0:39 as a parameter. 0:43 We can pass in an entire book object, 0:44 like the one we created earlier that's representing a Harry Potter book. 0:46 Then we just need to add the book object to the array stored in the library's books 0:50 property using a simple array method like push. 0:55 This can be a simple, one-line method. 0:58 addPatron() will work exactly the same way. 1:00 Open up the attached workspace to find an updated read me file with 1:03 new instructions. 1:06 If you'd like to continue working in your existing workspace, 1:07 check out the teacher's notes for the new directions. 1:10 Once you're done, or 1:12 if you need some help, check out the solution following this video. 1:14 Then we'll join back up to test out our new methods. 1:17
You need to sign up for Treehouse in order to download course files.Sign up