Instructor: After functions accepting functions as input,
let's now talk about functions returning functions.
And to learn how to do that, let's create a function
that creates different interview questions
for different jobs.
And this is how we're gonna do it.
For each job, we will return the function
that builds a string using the person's name as an input.
So basically, a function returning another function.
So function, let's call it interview question,
and it will accept a string which is a job.
And then in here, according to each of the different jobs,
we will return a different function which will then log
a question to the console.
So we're going to do an if l statement here.
So if the job is designer, okay,
then return, and here comes the fun part
because we will simply return a function
which will accept a name,
and then in here we put our code.
So this is basically an anonymous function, right?
So this function here, it's an anonymous function
because it doesn't have a name.
So all of this is what we're going to return
if the job is a designer.
So console.log and interview question for designer will be
first the name, because we will pass that into the function,
right, so here is the name parameter,
and then let's ask the person can you please explain
what UX design is?
So user experience design, okay.
And now, another job, let's say a teacher,
so teacher, and in here we again return a function,
an anonymous function again, which also accepts a name
and then a similar string, so console.log,
and then it's a teacher so let's ask the teacher
in interview what subject do you teach,
and then maybe the name of the person.
Okay, now we could continue this with a lot of jobs
or we could even have used a switch statement, right,
but I don't wanna over-complicate things here
because this is just for demonstration,
so I'll just leave these two and then here,
an else statement, and again we're going to return
a function and in here, we want again console
and then ask hello, then a name,
what do you do?
So just a random question if we don't know the job.
Alright, so this function here that we just wrote
will not just return a simple value as we usually do
with functions, right?
So instead, it returns an entire function
that we can then use later, and this is possible because
functions are always first class functions in JavaScript
because they are effectively objects.
So we simply actually return an object here, right?
It's simply an object that happens to be a function,
so let's now use this and remember this returns a function
so we have to store the result somewhere,
so let's create a variable, and now we'll call this
one a teacher question.
So this means it will be a question always for teachers.
So now interview question and then what I pass into it
is the string teacher.
And now, if I run this, then this teacher question
variable here will be the function that is returned
from the interview question when we pass teacher.
Where is it?
So here, so again this variable teacher question
down here will now be this function here, right?
This is just like when we store a function expression
in the variable, this is the exact same thing, right?
So let's now use this and I'm going to say
teacher question and the name John.
So I'm now calling this variable here, which is
a function, and I will put the name John into it,
here, right, because as we know, this function
accepts a name as an input.
So let's see what happens.
Alright, so what subject do you teach, John?
Exactly as expected.
So with this method, we can basically write
one generic function which is the interview question
in this case, and then create a bunch of more specific
functions based on that generic function, and in our case,
that's this function here, the teacher question,
which produces questions for teachers.
So we can now also create a function which produces
questions for designers, right?
Let's say designer question.
And then again we simply call the interview question
function, right, but this time, with the designer
as the argument.
And now we have a function which is for designers.
Let's console log this here as well,
so designer question, and again John.
Let's console log this.
Okay, so I misspelled something here, alright,
so it's question, okay.
So, John, can you please explain what UX design is?
So once again, we have now created a bunch of more
specific functions here, so the teacher question
and the designer question functions based on
our more generic function, which is
the interview question function.
We could now go ahead and use this as many times
as we want, so let's say designer question for Jane,
and for Mark, and for Mike,
and you get the point, right?
So we can now use this for as many, let's say designers,
as we want.
So Jane, Jane, Mark, and Mike, so all of them
will get this question.
Let me just show you now that we could also have done this
in a different way, so let's say,
interview question, okay,
and then teacher, and as you know, this will return
a function, right?
So this will return this function here,
and we don't even need to store it in a variable,
we can call it right away, so we can use the parentheses
once again and then put, let's say Mark in here.
So this looks a bit strange, but it works, and that's
because this is evaluated from left to right.
So first this part here, so the interview question function
gets called, it returns a function, and then with this here
we call the function that was returned before with Mark.
So let's see, and yes, this still works.
What subject do you teach, Mark?
Alright, so this is of course a really simple example,
but it shows you how useful this technique can be.
And it really takes some time to getting used to this
way of thinking and using functions, but once you
understood, it can be really powerful.
Okay, and once again, I really wanna encourage you
to try this out on your own, write your own functions,
and do some experiments with this new concepts
of first class functions.
And after that, you can just proceed with the course.
So see you then.