Hi there.
It's time for us to talk about objects, methods, in the previous video, we learn what is an object
and we learn that the values associated to the objects properties can be, if any, data type.
So we saw examples of strings, numbers, arrays.
We can also have other objects inside objects.
But not only that, we can also have functions.
So in this case, we are going to call them methods.
Methods are just like any other function, but inside objects they are extra useful because they can
use other object properties using the key word this.
So let me go to Visual Studio Code and I'm going to create an object for a student.
So open and close curly braces to indicate my object.
Now, inside here, we can add properties like first name.
And last name.
Now, if we want to have the full name instead of doing it ourselves, every time we need to use it,
we can create a method that is going to do this for us so I can create a new property called full.
Name.
And I'm going to assign a function to this property, so I'm going to write it a bit differently because
this time we don't need the name for the function because the name of the function is actually going
to be the property.
So we just need to open and close parentheses.
I'm not going to use any arguments for now because in this case, we can access the elements of this
object using the key word this.
So I'm just going to return.
This not first name I'm going to concatenate with an empty space and then I'm just going to get this
dot last name, so this is our method.
So now let's just save this.
Let's go to the console, refresh the page.
Now, as you can see, we have the student.
Object.
If we open this, we can see that we have the first name, which has a string associated, the last
name and the full name has this F indicated that this is a function.
So I can do student that.
First name, if I want to get the first name or I can do student that full name, which is going to
invoke the method we created.
So for this, we need to open and close parentheses.
By doing this, we're able to get the full name of the student.
Just one important thing I want to mention is that we can only use the dot notation, this one, to
invoke the methods.
We can't invoke it like this using braces.
This means that if you have a property that does not follow the variable name rules, you won't be able
to call this method.
Even if you could.
We should always follow those rules.
So now something interesting.
Did you know that everything in JavaScript is an object?
JavaScript is an object oriented programming language, so it treats basically everything as an object.
Objects, arrays, functions and everything except for the primitive data types are objects.
Let's remember that when we had an array, so I'm going to create an array called our.
Just to use as an example and if we do type of.
We can see that this is actually an object, so pretty much everything in JavaScript is an object.
So now looking back to a few things we've done, we can see how things work in JavaScript.
So when we do console, dot, log, what are we actually doing?
Log is a method of the console object.
So this is why we use the DOT.
And in this case, this method has an argument, which is the text we want to send to the console when
we use math.
Dot round round is a method.
Of the math object, so now things start to be a bit more clear.
Speaking of which, let me clear this and let me call the math object just so we can see something interesting.
If I do this and open this, you can see that we have a lot of methods in this object.
We also have a few regular properties.
So, for example, we have the PPI number.
So if I do math.
Not pi, it returns the PI number, which is a constant it is never going to change.
So this is just a regular property.
But then we have a few methods that we can invoke with the DOT notation so we can do like math.
That round.
Which is just invoking the round method of this object, and now we need an argument, which is the
number we are trying to round when I do like this, then it's returning the result.
I have a few other examples here, like when we select age HTML elements and get their content, what
we're doing here is using the get element by the method.
So the document object generated by the DOM has a method to select an element, which is this one.
So by performing this operation, this is going to return the object representing the HTML element with
just selected and this element has a property called inner as HTML.
This is why pretty much everything we do in JavaScript is going to use the dot notation because we are
going to be working with objects most of the times.
I have another example here.
So we create an array called courses, and when we do push, what is push is a method that can be applied
to a race.
So I know this is getting a bit more complex now.
So this is why I'm not going to go any further now.
But I bet that a lot of things are starting to be more clear in your head.
Let's do this little by little so we don't get overwhelmed.
But if you want to learn more about objects, you can click on this link and you have a ton of information
you can learn from.
So that was all for this lesson.
Now we are ready to do another list of exercises and put all this into practice.
I'll see you then.