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