Jonas: Let's continue learning about objects,
this time diving deeper into object methods.
So we learned in the last lecture that an object
can hold different types of data, right?
So including a raise, and even other objects,
but we can also even attach functions to objects.
And these functions are then called methods.
So let's see why and how we can create methods.
And for that, let's actually go back and grab
our John object so we don't have to rewrite it all again.
So here we have our object.
So what we want to do here is to add the function,
so a method, to this John object in order
to calculate the age of John.
So we don't want to have the age hard coded in the object
because it changes every year,
and what stays constant, of course, is the year.
And so instead of having the age hard coded here, an object,
we have a function that calculates that age.
So all we have to do is to basically define another key.
So calcAge, which stands for calculate of course,
so calcAge, and now just like before, we define the value.
And value here is a function expression, basically.
So we write function and then the birth year.
Alright, and that's it.
Now all we have to do is to write our function by it.
And now maybe it makes a bit more sense why we learned
about function expressions and function declarations before.
So this here is clearly a function expression, right?
So it's like we have a function without the name,
just with the argument which we then assign to a variable,
which is in this case is calcAge.
And so this function here is now a method of John.
So now all we have to do is to return the year,
just like we did before, minus the birth year.
Okay, simple right?
Now how do you think we are going to call this?
Well, it's very simple again.
And let's console log the result of calling that function.
And so, the way we call it is just the same way
that we access properties.
So, John and then dot.
Instead of accessing one of the properties,
we simple access calcAge.
Then it's just a normal function call.
So calcAge and then we pass in the year,
so 1990 in this example.
And so now, we will get the age which is 28, great.
So that's a very basic functionality of methods.
And remember that in the array lecture
I showed you methods for an array,
and so that means that arrays are actually also objects
because that's the only way that they can have methods,
right, because only objects have methods.
Arrays have all these functions to change them,
like push and pop, remember?
We will dive deeper into why things work like this
and how we can create this same functionality
for our objects later in the course.
For now we're just talking about the method basics.
So just to learn how methods really work.
Now continuing with our John example here,
I'm sure that you noticed that the year of birth
of 1990, this one here,
that we passed in as an argument of the function
is actually already defined in the John object itself.
Right, so it's already here, in fact.
So what if instead of having to
pass it into this function here again,
we could access that property
right from this method inside of this object?
Well, it turns out that we can
and that's because in every object,
JavaScript gives us a special keyword called "this".
So let's try that out.
Instead of having to pass in the birth year
which is already defined here,
we can say this dot birth year.
So "this", what it means here in this context is John.
"This" means this object, the present, the current object.
So when we call this method here, this dot birth year,
will mean John dot birth year.
So let's try that out again and let's just change it here
so that our output here is going to change.
So let's put 92 here now, and if you reload,
now we see that he's now 26.
What that means is that "this" actually works.
So we can access now this dot birth year,
because "this", again, means simply the current object.
So basically the object on which we have calcAge.
Okay, so that's great.
But let's actually take it even one step further.
So what if you actually wanted to store this result
right into the John object?
Well, we could do it like this.
So we would first calculate the age, so just like this.
Then, we would say, john dot age equals the age.
Right?
Or we could actually do it all in one line.
So something like this.
Okay, so we call john dot calcAge,
which will return the age,
and then we set john dot age to that result.
So remember that we can set properties like this
just as we learned in the last lecture, right?
But we actually can do even better.
So if you can access a property
in a current object like this here,
well, that it means that we can also set it, right?
And so instead of returning this number here,
we can simply say this dot age should be equal to 2018
minus this dot birth year.
So now all we have to do is to call this function, right?
Then we can also log the entire object to the console,
just to see if the result is really there.
And, let's see it.
And indeed, here we have the age of 26.
And so, this worked.
So the "this" keyword is something really powerful
and really important that you will have to master
throughout your journey of learning JavaScript.
So remember in this lecture we went from
actually having to pass in the birth year as
an argument of the function,
to being able to read the birth year directly
from the John object using the "this",
and then from there we went even one step further
and instead of returning the age result
we also stored it right into the object.
And so that's the best we can do
and that's the way it's supposed to be working.
So if you understand this basic functionality
of methods and the "this" keyword,
then you're well on your way
of mastering objects later in this course.
Because this is actually a very complex subject
and we will dive much much deeper
into it later in the course.
But for now, I really just want you to understand
that an object has a special "this" keyword
which basically points to itself,
and I want you to understand
that you can use methods like this.
So, just like I showed you in this video.
Alright, make sense?
So I think you're now ready
for yet another coding challenge.
I hope that you like these.
I think they're perfect for you to practice your skills
and what you just learned.
So hopefully I see you there in a second.