Jonas: Let's do the opposite of the last lecture
and create a function that returns a new function.
So I'm gonna write a very simple greet function here,
because this is really just to demonstrate this point here
as easily as possible.
So here we can pass in a greeting like hi
or hello or something like that.
And then all this function will do
is to return a new function.
So as always,
we write return and then simply a new function value.
And this new function that we return
will also have a parameter so it will be able
to receive arguments. Right?
And so what this function here will do,
is it will simply look to the console,
basically the greeting, and then the name of the person
that we passed into this function.
So let's just write this
and it will make sense when we see this in action.
I'm sure it looks a bit weird right now.
And now let's actually use the greet function.
So let's say greet
with hey and so 'hey' is the greeting here.
Now what will be the result of this function call?
Well, it's gonna be this function, right?
And so let's store the result of this function call,
which will be as we just said, that function,
I'm gonna call this one greeterHey.
So this value here now is actually now a function.
Okay. And essentially it is this function.
So what this means is that we can now call
this greeter function just as if it was any other function
that we defined ourselves.
So let's say greeterHey Jonah's
and let's call it another time.
Greeter Hey Steven
and now I'm gonna run it.
And indeed we get Hey Jonas and Hey Steven.
And so this worked again because this greeterHey,
it's essentially this.
And so now we're calling it with the argument of Jonas.
And so this name here is then of course,
this name that we specified here.
Now, the greeting is still coming from
this greet function here.
And in case you're wondering why that actually works,
it is because of something called a closure.
Now closures are a very complex and difficult
to understand mechanism that's part of JavaScript.
And so there are two separate videos at the end
of the section about closures.
So closures is one of the
most misunderstood topics in JavaScript.
So don't worry about that Now,
what matters here is that our first function a greet
returned a new function that we stored into this variable.
And so this variable is now just a function that we can call
as we did here again sing the parenthesis syntax.
And of course we could also do it all in one go.
So we can again, call greet. let's say hello now.
And so again this year is now a function.
Okay so if this is a function,
we can immediately call it, right? And so we call like this,
and then we passed into name which is the argument
of this function here, right? And so even though
this looks a bit weird, it is gonna to work.
And so we get hello Jonas now.All right.
And now this example might look a bit weird
and unnecessary for you Like what's the point
of having functions returning other functions?
Well, this will actually become extremely
useful in some situations.
And especially if we're using a really important
programming paradigm called functional programming.
And I think I've mentioned it sometimes throughout
the course and we're actually gonna
look at functional programming by the end of the course.
Maybe not by the time I first published the course,
but certainly some point later.
Anyway make sure to understand this here
and especially this line of code.
And if you understand this, then I'm sure
you understood everything what happened here.
Now, just as a small challenge to finish here,
try to rewrite this function here
using only arrow functions.
And believe me that's gonna look a little bit confusing,
but I'm sure you can do it.
So just take some time and try to rewrite
that greet function using arrow functions.
And I see you here in a minute.
Okay,
so let's
see how it's done
so greet
Just gonna call it R,
so this one takes again,
greeting as an argument and it returns a new function.
So this means that it only has one line of code,
which returns something.
So all we need is this arrow.
And then we don't even need the curly braces
and we don't need to return a statement.
So what are we gonna return from here?
A new function.
So again, an arrow function and so another arrow here,
and then simply this
let's give it a safe and a try
So greet
R
and let's use hi, this time and yeah,
we get hi Jonas,
okay?
So this is an even shorter way
of writing the same function but in my opinion,
it's actually a lot more confusing.
And that's why I wrote it using
this more traditional way here.
But in the end,
it's also simply one arrow function
returning another arrow function.
And so that's the essence of this video,
which I hope was clear to you.
And if so, then let's move on right to the next one.