- To describe our real world objects,
the backpack in JavaScript, we can use a JavaScript object.
JavaScript objects are collections of data
and functionality stored as properties
and methods that describe the object
and what it can do.
To define an object, to create it,
I first need a variable to hold the object.
The modern convention here is to create a constant
and we'll talk more about variables
and constants later on in the course.
So for now, just think of it
as a box that we put the object inside.
I give the constant the name, backpack,
so we can refer back to it.
So anytime we say backpack,
we are now referring to the object.
And then I use the equal symbol
to assign a value to the constant,
essentially filling the box.
What I fill the box with is these two curly brackets.
The curly brackets say this is a JavaScript object.
And currently the JavaScript object is empty.
So I need to populate my object with some data.
This is done using properties.
Each property is a name value pair, separated by a colon.
The value can be many different things.
It can be a text string inside quotation marks
like right here.
It can be a number,
or a Boolean true false value, an array,
or it can even be another object,
as you see in the strap length example.
An object can have as many properties as you like.
You separate them using a comma.
And the convention is to add each new property
on its own line so it's easy to read
what's going on inside the object.
An object can also have methods used
to change the properties of the object.
These methods are also named value pairs.
Only in a method, the value is a function
or a call to a function.
Calling the method from outside the object triggers
whatever function is contained within that method.
In this example, we have the toggleLid method.
It contains a function that grabs a value for lidStatus,
and then says this, so the current object's, lidOpen,
should be changed to whatever's contained
inside the lidStatus.
You'll remember previously in the course
I used this exact function to change the value
of the lidOpen property from false to true.
Finally, when you work with objects,
you'll often see the, this, keyword used.
The this keyword, simply refers to the current object.
So we're saying this object right here.
That way we're referring to the context
of the current object and not some other object.
So this lidOpen, lidStatus
just means the current object's
lidOpen property, should be changed
to the value of lidStatus.
What you see here is a JavaScript object version
of my real world object, the backpack.
It's properties describe what makes this backpack unique.
And the methods allow us to change the changeable properties
like whether the lid is open or closed
and how long the strap length is.