- Now that we have an overview,
let's dig a little bit deeper
and really understand how an object fits together.
The object needs somewhere to live
and it needs a name.
For this, we use a container called a variable.
And in this case, it's a constant variable.
The const keyword says, this is a constant.
The name is the name we use to reference
this particular container.
And whatever's on the right hand side
of the equal symbol is what goes inside the container,
what is assigned to that container
or to that variable.
In this case, an object.
Now this is a constant,
meaning while we can change the properties
of the object inside the container,
we can't remove or replace the object from the container.
If we try to do so by calling the constant name
and using the equal symbol to set the contents
to something else,
the browser will tell us you can't do that.
This is a constant.
Let me show you.
In the exercise files for this movie 0303,
we have the backpack object.
If I go into my browser and go to the console,
I can console log out the backpack
by just typing out backpack.
Here, you see the full backpack object
and we have access to all this data.
Now we can try to assign something else
to the backpack constant.
So I'll say backpack equals, whoops.
Backpack equals five.
Now this is a variable so technically
I should now remove the object entirely
and just put the number five in its place.
But if I run this, I get an uncaught type error,
assignment to constant variables, saying this is a constant.
You can't do this.
You can't change its contents.
And this is the reason why we use constants for objects.
Once we've created an object, we might want to
change its properties, but we never want to change it
to something entirely different.
Speaking of properties, I can change its properties.
So for example, right now, the volume is at the 30.
I can then say backpack dot volume and then set it to
50 and then just call backpack again.
And you'll see now the volume is 50 instead.
So even though it's in the constant,
the properties are not constant.
I just can't assign something altogether different
to that constant.
That way I can't accidentally destroy my objects
while working with them.