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
Here an explanation for how we'll build the constructor methods for the three classes used in our library application.
Instructions for Workspace
Your instructions are as follows:
1) In the Library.js file, add a constructor method to the Library class. In the constructor method, initialize the following properties: books
, patrons
.
2) In the Book.js file, add a constructor method to the Book class. In the constructor method, initialize the following properties: title
, author
, isbn
.
3) In the Patron.js file, add a constructor method to the Patron class. In the constructor method, initialize the following properties: name
, email
, currentBook
.
Don't forget to add parameters for any values that need to be passed in to each of the constructor methods.
-
0:00
Welcome back, Ashley here.
-
0:01
Let's move forward with our library app.
-
0:03
So far we've declared three classes, library, book and
-
0:06
patron, each one in their own js file.
-
0:09
When I'm building out an object oriented app, I like to follow my rough plan and
-
0:13
build out my constructor methods for each class first.
-
0:16
As a quick recap a constructor method is where we tell the class, the properties.
-
0:20
And the property's initial values that any new object
-
0:23
of that class type should have available to them.
-
0:27
Building the constructor methods first let's me instantiate a few test objects
-
0:30
right off the bat.
-
0:31
That I can plug some data into and play with as I go.
-
0:34
This gives me a way to make sure that my code is working properly along the way.
-
0:38
So that's what we'll be doing next.
-
0:40
Building out the constructor methods for the library, book and patron classes.
-
0:44
Let's start with the library class.
-
0:46
To recap, according to our rough plan, the library class has two properties,
-
0:50
books and patrons.
-
0:51
These are going to be initialized to empty arrays in our constructor method.
-
0:55
That's because the library doesn't have any books or
-
0:57
patrons yet, it's a clean slate.
-
0:59
A user of our library system can add books and
-
1:02
patrons to these arrays, through the methods relayed out in our rough.
-
1:06
AddBook and addPatron.
-
1:08
This means we don't have to pass anything into our constructor method for
-
1:11
the library class.
-
1:13
As for our book class, we're going to be initializing a few properties
-
1:16
in the constructor method as well.
-
1:18
Just so you know,
-
1:19
properties don't have to be initialized inside a constructor method.
-
1:23
They can be initialized, like any variable, in a method on a class as well.
-
1:27
If that's the case, that means an object of that class type
-
1:30
will only have those properties if that method is called on.
-
1:35
Our rough plan says that the book class will have the properties for
-
1:37
title, author and isbn.
-
1:40
All of these values will need to be passed in.
-
1:43
Finally, let's talk about the patron class.
-
1:45
The properties initialized in the constructor method are name,
-
1:48
email and currentBook.
-
1:50
The values for the name and
-
1:51
email properties will be passed in when a patron object is created.
-
1:55
In the real world, the patron will be providing that information to the library
-
1:59
employee who keys it into the system.
-
2:01
The currentBook property, however, is a little different.
-
2:04
A patron won't have checked out a book when they are first entered into
-
2:07
the system.
-
2:08
So when the new patron object is created,
-
2:10
the value of the currentBook property should probably just be null.
-
2:14
We can set that directly inside the constructor method
-
2:16
without passing in a value.
-
2:18
Later on, when a patron does borrow a book, the checkOut method can update
-
2:22
the current book property so it holds the book that the patron is borrowing.
-
2:26
Let's add these constructor methods to our class files, then afterwards we can create
-
2:30
some test objects and see what they look like and how they work so far.
-
2:34
Head to the attached work space to see the updated read me with new instructions.
-
2:38
Or see the teachers notes attached to this video.
-
2:41
Once you're finished or
-
2:41
if you're having trouble, check out the solution following this video.
You need to sign up for Treehouse in order to download course files.
Sign up