"CSS Basics (2014)" was retired on July 12, 2021. You are now viewing the recommended replacement.
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 Object-Oriented JavaScript!
You have completed Object-Oriented JavaScript!
Preview
Learn how to instantiate new objects from classes.
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
Now that we've finished our constructor
method, we're ready to create or
0:00
instantiate a pet object.
0:04
Creating an instance of a class is really
easy, and you might have seen this syntax
0:05
before, perhaps when using the JavaScript
date object, or when creating a new array.
0:10
First, declare a variable
that will hold your object,
0:14
set it equal to the keyword new,
followed by the class name.
0:17
Make sure the first letter of
the class name is capitalized,
0:20
just like when we declared the Pet class.
0:24
And remember, we'll want to pass in
the correct parameters, age, animal, and
0:26
breed, to the constructor
method inside the class.
0:30
In this example,
those values will be dog, 1, and pug.
0:33
Take a moment to practice creating
some other instances of the Pet class.
0:40
You can use the remaining object literals,
or
0:44
you can feel free to
model Pets of your own.
0:47
I'm going to show this process again using
one of the other Pets from the object
0:49
literal examples.
0:54
So I will do const vera = new Pet, and
0:55
then I'll pass in
the parameters which are dog,
0:58
8 for her age, and then border collie,
which is her breed.
1:02
Great, now let's write a line to
output the ernie object, save,
1:08
and then hop down to the console to
run the file and see what we find.
1:13
I'll make the console area a little larger
so we have some more room to work with.
1:25
To run the file, we're going to use node.
1:29
Your workspace is all set up to use node,
so running the file is simple.
1:31
If you're not using workspaces,
check the teacher's notes for
1:35
some information on how to install
node on your local machine.
1:38
In the console, type the word node
followed by the name of the file.
1:43
Which, if you've been following along,
is Pet.js, and then hit Enter.
1:47
Your output will appear in the console.
1:53
You should be able to see
all of the properties and
1:54
their values attached to the ernie object.
1:57
As you can see here,
we have animal, age, and breed.
1:59
What do you see in your console,
does it match with what you see here?
2:05
If you got an error, go back and
follow along with this video again.
2:08
You can also post about anything you're
struggling with to the Treehouse
2:12
community.
2:15
Not only will this help you
get the answers you need, but
2:16
it will help other students who
might be having similar struggles.
2:19
A huge part of working as a developer is
knowing how to find answers online and
2:22
helping others when you can.
2:27
No developer knows everything,
and that's why we work together.
2:28
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