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
Object literals are an important foundation for understanding object-oriented programming in JavaScript. Get to know object literals and the different components of objects.
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
[MUSIC]
0:00
Hey there, welcome back.
0:04
Congratulations on finishing
the first stage of this course.
0:06
I hope you're excited to keep learning
about object oriented JavaScript.
0:09
In the last stage, we started thinking
conceptually about what objects are and
0:14
how we use them.
0:18
For this section of the course,
0:19
we're going to continue our chat
by talking about object literals.
0:20
As a reminder, objects give us a way to
package information about something we
0:24
want to use in our code.
0:28
This package is made up of a group
of properties and functions,
0:30
called methods, that work together to
represent something in our program.
0:33
Properties are like object specific
variables that store information
0:38
in a series of key value pairs.
0:41
Methods are object specific functions
that let your object do something or
0:44
let something be done to it.
0:48
Object literals are one way
to create an object and
0:50
they're really great when you're
modeling only one single specific thing.
0:52
Let's look at an example.
0:57
At home I have a pet named Ernie, and
0:59
I've created an object literal
to model him in my code.
1:01
To start,
I declared a variable called ernie and
1:04
set it equal to an empty
set of curly braces.
1:07
Curly braces are how we notate an object.
1:10
Remember, the curly braces you use for
1:13
objects are different than the square
brackets you use to create an array.
1:15
Inside the curly braces, I added
a series of key value pairs that make up
1:19
the three properties and
one method inside this object literal.
1:23
On the first line inside the curly braces,
1:28
you will see the word animal, followed
by a colon, followed by the string dog.
1:30
This is a key value pair, and
1:34
it is representing the animal
property of this object literal.
1:36
The key is the name of the property, and
1:39
the value of the property
follows after the colon.
1:41
Property values don't
have to just be strings.
1:44
They can also be numbers,
Booleans and even functions.
1:47
In our example here,
we've got three properties,
1:51
animal, age and breed.
1:53
An object literal's properties and
methods are always separated by commas.
1:56
Each new property or method is on
its own line for readability.
2:00
But technically, you don't have to
do that for the syntax to be right.
2:04
On the line after the breed property,
you'll see another property called bark.
2:08
But instead of a string value after the
colon, there is a function declaration.
2:12
This is one way to add
methods to object literals.
2:16
The property key is
the name of the method,
2:19
and that's the name you'll
use when you want to call it.
2:21
Putting all of these properties and
methods into a package and
2:24
attaching it to a variable
is called encapsulation.
2:27
That's an important term to remember, and
2:30
you'll probably hear it a lot
as you progress as a developer.
2:32
Now that you know all about creating
object literals, properties and
2:36
methods, I bet you're super ready
to start using these skills.
2:39
Let's practice them a bit in
a code challenge after this video.
2:42
After that, come back and join me to
learn all about how we access and
2:45
use properties and methods.
2:49
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