Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Practice Object Interaction!
You have completed Practice Object Interaction!
Preview
How can we use objects to model a real-life library in our JavaScript code? Let's put together a rough plan of attack for our library application.
This video doesn't have any notes.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Let's dive in,
0:00
we've been asked to create a basic library
app using object oriented principles.
0:01
That means we'll be using classes and
0:05
objects to manage the different
parts of our library system.
0:07
The first thing we'll do is come
up with a rough plan of attack.
0:10
Keep in mind plans always change
as we move through programming and
0:13
app, but that's okay,
0:16
it's still a good idea to have a rough
outline of how you'll lay things out.
0:17
Having a basic plan creates
a good place to start.
0:21
But it's almost guaranteed that as you
get more familiar with the problem or
0:24
challenge, you'll think of
a better way to solve it, or
0:27
a new important feature
that needs to be included.
0:30
With that in mind let's think about a
real-life library, what does a library do?
0:33
How can we translate a library
into a JavaScript class?
0:37
Well, a real-life library has many
books and it has many patrons.
0:39
If we had a library class then books and
0:45
patrons are two properties that we can add
to the library class constructor method.
0:47
Since a library has many books and
0:52
many patrons, we'll initialize
these properties to arrays.
0:53
As a reminder,
0:57
when we initialize properties in
a classes constructor method, any object
0:58
created of that class type will have those
properties automatically available on it.
1:02
But how will books and
patrons be represented in our code?
1:07
Well, those can be modeled into classes
too, they both definitely have properties.
1:10
Books have a tremendous amount of
data like title, author, ISBN,
1:15
and publication date.
1:19
Patrons have names and
contact information.
1:21
This contact information like
an email address, for example,
1:23
can also be use to differentiate between
patrons who frequent our library.
1:26
Another property we probably need to have
on our patron class, is something like,
1:30
current, or active book.
1:34
We need to know what book, if any, a
patron has checked out at any given time.
1:36
What about methods?
1:41
What behaviors do libraries, books, and
1:42
patrons have that we can model in our
library, book and patron classes?
1:44
Let's think about the fundamentals we
need to make our library system run.
1:48
To start, what about adding a book or
a new patron to the library?
1:53
If the book and patron properties
in the library class are arrays,
1:57
how do we get book objects and
patron objects into these arrays?
2:00
One way is to write addBook and
addPatron methods.
2:04
As for our book class, I'm not sure what
methods or behaviors our books might have.
2:09
Our book class is mostly used as a way to
store information about each book object.
2:13
This could change as we flesh out the book
class and get deeper into building our
2:18
library system but for now,
I can`t think of any methods to add.
2:22
On the other hand, we know that
patron need need to be able to check
2:26
book without books out and return them.
2:29
So we need to have methods in
our patron class that mimic
2:31
this behavior in our code.
2:33
For now, this is a great place to start.
2:36
To recap, we have three classes library,
book, and patron.
2:39
The library has two properties,
books and patrons,
2:42
which will hold arrays of book objects and
patron objects, respectively.
2:45
The library class will also
have methods to add book and
2:49
patron objects to these properties.
2:52
The book class will have properties for
title, author, and ISBN, but
2:55
no methods yet.
2:58
The patron class will have properties for
name, email address, and current book.
3:00
It will also have methods for
checking out a book and returning it.
3:03
Now that we've laid out a rough plan,
we can start coding.
3:08
The first thing we have to
do is declare our classes.
3:12
You'll see a workspace is attached
to this video that includes a few
3:15
files with no code inside.
3:17
There's a library.js file,
a book.js file and a patron.js file.
3:18
The names of these files are capitalized,
3:24
just like how the name of a class is
capitalized in the class declaration.
3:26
This is a good clue for other developers
who might work on your project.
3:30
Each class declaration should
go in the associated file, so
3:34
there is only class per file,
this is a good habit.
3:37
It helps keep your code organized and
neat.
3:40
There's also an index.html file that
loads up each of our JS files and
3:42
a readme file with more
instructions on how to proceed.
3:47
Open up the workspace,
check out the readme and start coding.
3:50
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up