Now let's dive back
into our app.js file here.
And for this,
I'll also switch back to app.js here in index.html.
We did not learn a lot about values, variables, functions,
parameters, return values, and all these important concepts,
which you are encounter all the time
in JavaScript development.
To come to an end in this section
and lay a solid foundation for the upcoming sections,
where we are going to dive deeper,
I wanna talk about another area where we can use functions.
Remember objects?
Of course you do,
we used them in the exercise a few minutes ago.
In objects, up to this point,
we grouped together variables,
if you want to call it like this.
We have related values grouped together into an object,
and we have labels to then identify these values.
Now it is also not uncommon
that you don't just want to have values like that
in an object,
but that you also wanna have functions
inside of an object.
So just as you can basically put variables into an object,
you can also put functions into that object.
And that makes sense
if a certain function contains code
that works together with the values in that object
or that logically belongs to this kind of object.
And to give you an example,
I'll add a brand new object down there
and I will name it person
and be created, as we learned it, with curly braces.
And now we could add different label value pairs,
for example, for storing a name of that person like Max,
but I want to focus on adding a function here.
And adding a function here works like this.
We could add a greet function
where this person then says hello to us like this.
So basically as we define a function,
we also add a name, parentheses,
maybe with parameters if we need them,
and then the curly braces,
and we do the same here.
The only difference is
that we don't have the function keyword
just as we don't have the let keyword
in front of our properties.
So it's basically the same, just for a function.
We could accept parameters here,
but we could also say that this is a very simple function,
which then just says hello,
by using the built-in alert command.
That's how we could add such a function to an object.
And if we add a function to an object,
we typically call that a method.
So a function in an object is called a method
just as a variable in an object is called a property.
So here we call this a property,
we call this a method.
But it's technically still is a function, of course.
And here we now, again, also see an example for a function,
or a method, that does not take any parameters
and that does not return anything.
Because for this function,
and the kind of operation it's performing,
we don't need to return anything.
Just executing another function is all we need here.
And in that case,
it's perfectly fine to define a function like this
without return value and without parameters,
no matter if it's a method or not,
you can write functions without parameters
and/or without return values.
But now the question is,
how do we execute this function in this object?
We don't execute it like this.
That would be the function name,
but just as you can't access the property like this,
you also can't access a function just like this,
that won't work.
Instead we do access a function in an object,
so a method,
just as we access other values in an object,
by using that dot notation.
We can access the object name, person, and then a dot
and then in here, we have this greet method.
And we see the difference between properties
and methods here in this suggestions pop-up.
Properties have this blue box,
methods have this purple box,
which is shaped differently.
But then we still execute it as we learned it,
so by using its name and adding parenthesis thereafter.
And if this function would need any parameters,
we could still pass the parameter values here
between these parentheses.
But here we don't need that.
And with this, if we saved that and I reload,
I still get my old boxes and then I get this hello box,
which is coming from inside my method.
And that is something which we'll also need
a little bit later in the course, but most importantly,
something which we will use indirectly
on certain built-in objects.
And in the next lecture, I'll explain what I mean with that.