Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Update and add new properties to object literals using dot and bracket notation.
Practice Opportunities
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
In this video, you'll be learning
about changing properties and
0:00
adding them to object literals.
0:03
Everything you're learning in this
stage of the course is to get you
0:05
really comfortable with the basics
of objects and object syntax.
0:08
Object literals are just one
way to create objects, but
0:12
they're a great way to learn the basics.
0:14
So that you're ready to hit the ground
running with more advanced object
0:16
discussions later on in this course.
0:19
Remember, object oriented programming
is about designing programs that use
0:22
objects for
the primary logic of the program.
0:26
To do that, you need to have
a good grasp of the syntax for
0:29
how we create and interact with objects.
0:32
Now that we know how to access properties
using dot and bracket notation,
0:35
we can build on that knowledge to
change or manipulate properties.
0:39
We can also use this knowledge to add
new properties to object literals.
0:43
We're going to keep working with the ernie
object literal from the previous two
0:47
videos.
0:50
Ernie is a year old right now,
but time will go on, and
0:51
he will eventually get older.
0:54
When he turns two,
we'll need to update his age property.
0:55
Remember when I referred to properties
as object specific variables?
1:00
Well, just like we would update a regular
variable, we can update a property.
1:04
You can use either dot or
1:09
bracket notation to access the property,
then simply set it equal to the new value.
1:10
For dot notation,
that means ernie.age = 2.
1:16
To change the age property
using bracket notation,
1:23
you'll do ernie['age'] = 2.
1:28
Interestingly, for
both dot and bracket notation,
1:32
you would use the same syntax to add
a property to an object literal.
1:36
For example, Ernie doesn't
currently have a color property.
1:42
If we wanted to give him one,
1:46
we would simply do ernie.color = 'black'.
1:49
Let's check what's going on inside our
object literal by logging the value of
1:55
ernie to the console and
then running our file.
1:59
I'm gonna save it and
then go back down here and run the file.
2:06
Perfect, as we can see
down in the console,
2:13
Ernie now has a new property called color,
great job.
2:16
In this stage of the course,
2:20
you learned all about working
with the object literals.
2:21
You mastered dot and bracket notation, and
2:24
you learned how to add properties
to object literals as well.
2:26
Check out the teacher's notes to find
some additional opportunities for
2:30
practicing these concepts.
2:33
And then join me in the next stage to
continue your object oriented journey.
2:34
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