This lesson, we're jumping right into coding, but before we do, just a little overview on what JavaScript
objects are and if you do understand JavaScript objects, well, this is the key to understanding JavaScript
because almost everything in JavaScript is an object.
So it comes to writing code.
There's always more than one way to create a solution.
And we're going to look at that in more detail as we progress through the lessons when we create certain
code.
And then we're going to make it within an object oriented programming format later on in the course.
So the object is a collection of properties of properties associated between a name and a value, and
it's basically a way to get information and store information.
So think of it as a more complex variable where you've got access to tons of information and objects
can be very, very large.
So a property can also contain.
So the object can also to contain a function.
And then within the object, this function functions the same way as a function, but it's known as
a method.
So that's a the two key things to keep in mind about objects is that they're very versatile and they
can get very complex so they can hold information as well as methods in order to create an object.
There is a number of ways to create objects.
So typically you might be familiar with literal notation where just as we would with a variable, where
we create a variable and then we've got the curly brackets and then we've got a name and a value, a
name and a value, and then we can return that information back from the object.
So how about we try this out and we're going to create some simple objects in this lesson so you can
open up your editor and you can create your person object as well as create a just a regular object.
So create a variable or a value and then associate some names with values within it and then return
that information back using your console and then make it a little bit more complex where you could
use your first name, your last name, assign an ID and then include a method and then use some of the
data that's contained within that object and then return that back when we call back the method.
So in this example, we've got person details and it's returning back the information contained within
the object.
So go ahead, possibly try this out and I'll walk you through these two solutions coming up.
So now let's have some fun with JavaScript.
Open up your editor and I'm going to hide the rest of the sidebar because we're only going to be working
on the Ops area.
And this is the crucial part of what we're looking at within the lesson.
Of course, what we saw earlier is how to create a basic object so you can give it a name.
I'm going to call it my object.
So we're short and then anything within the object is contained within the curly brackets.
So if we want to add content into the object, we can have whatever we want.
So give it a name and then a value associated with it so we can have hello world or whatever string,
no array object as well as function associated with this deemed value.
So if we have something like greet and again we could create another string, we could have a number,
we could also have an array within the object.
So this is all simple data types.
So then this array can contain different values as complex as you want.
Within the object, we can have an object within an object.
So this is all indicated the same way and then this object would be again broken down.
So let's refresh and look at how the object looks within the browser.
So we've got all of that great stuff contained within the object.
And as you can see, there is a ton of information and we can go really deep when we are creating these
objects as well.
If we want it to return the information or if we want to add to this object, it's relatively easy to
do what we just reference that mean object and then we can use the dot notation to add content to it.
So if I want to have add, I could say hi, add it.
So there's a number of different ways to update and to add so you can use the dot notation or you can
use the bracket notation.
So all of this is going to be the same.
And within the bracket notation, it's the square brackets.
So it looks a little bit like an array.
And then here's where you've got the name that's associated with it and then the value is on the right
hand side.
So now if I list out my object, you're going to see that all of this information that I've just added
has now been added.
And so there's the ad which says, hi, we've got the test, one which has a value of two, and then
you can update these as well.
So my object, Dorte.
And so if we wanted to change.
And we can change it to a number of one or two, and then we can list out the object again and you can
see that it's changed.
So it's been updated within the object.
So those are the primary ways to create an object and then add content into the object.
And we also saw one more example where we've got another object and this one was a person and it actually
returned back some details.
So let's create that one as well.
And you can use VAR and you can also use const as well.
So depending on the scope that you want to create, because the way that objects work when JavaScript
they reference an instance.
So even though it's const, we still have the ability to update the values that are contained within
there because we're not actually updating the object itself, so we're not updating the object.
So if we are reassigning this object to something entirely different, like a string or something like
that, then we would throw an error.
But in this case, we're still able to make the updates because it's referencing a part that's referenced
within the memory.
So we are able to use const in order to declare the object.
So even if I was to take my object and if I do something like this might object equals test, I'm going
to throw an error because we're using a const.
But everything else, if we're working within the names and the values are contained within the object,
we're still OK and we're good to go.
So let's do that last one.
We're we're including the method.
So creating a person and you could use your own name as well.
So we've got first and from my case, that's Lawrence.
And then the last name.
You can throw in a number there if you want as well.
And this is the most important one where we're going to be attaching.
So we've got the name and the value on the right hand side is going to be a function and this function
is going to be returning back some content.
So using return and we're going to use a template literals for using the tactics that's just above the
tab to the left of the one on your keyboard or on most keyboards.
And this gives us the ability to add in variables so we can reference the content of the object using
this.
And because this one has a last name, so do last name.
And then here we can write the rest of the string that we want to output.
So again, we want to maybe reference the first name here, dollar sign and there are brackets and then
the ID.
So this is just regular string content here.
The reference in the objects, we're going to add the ID.
So lastly, so let's see what happens.
So now we've got another one that we've created called person.
If we want to get the first name or the last name, we can just do first.
It returns back first name, person last returns back, last name so we can get that information and
if we want to get personal and details.
So this is a function.
So we want to invoke the function, use the round of brackets to invoke the function and you can see
that it returns back that template literal, that string there we just constructed using the content
that's contained within the object.
So go ahead, try this out.
And we've got some more really cool things with JavaScript coming up.