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