Lesson Description
The "Callbacks & Higher Order Functions" Lesson is part of the full, JavaScript: The Hard Parts, v3 course featured in this preview video. Here's what you'd learn in this lesson:
Will believes callbacks and higher-order functions are two of the most misunderstood concepts in JavaScript. They are critical components in functional programming, enabling more complex function composition and useful function utilities, such as map and reduce.
Transcript from the "Callbacks & Higher Order Functions" Lesson
[00:00:00]
>> Will Sentance: So, here we go, callback tower of functions, one of the most misunderstood concepts in JavaScript, but if we understand it, we can do these pro-level functions like map, filter, reduce. I would recommend, if you want to go into those in more detail, check out the functional programming hard parts where we do use reduce in very interesting ways. Reduce is the most flexible function that can, I'm sure some of you will have encountered, do seemingly things that you would never have thought of as a reduction but actually can be treated as such, but that will be part of the functional programming hard part.
[00:00:35]
Here you can nevertheless see the underlying pattern that's being used later on and reduced. We do this, we understand our harder functions, higher-order functions, we can make our code more declarative and more readable. OK. So, here we go, but it all starts with, why do we even have functions? Hold on, you might be thinking, wow, why are we going through things so procedurally, step-by-step, it is what enables us to do the hard things to come.
[00:01:05]
Well, let's go even more procedurally, and we are going to declare, using the old style function keyword. Now you might be like, I don't tend to use that so much nowadays, we'll see the new arrow function declaration style in a moment. I will tend to use the function keyword throughout hard parts, primarily because for readability, it does let us know there's a function. But we are going to use that syntax here, so for teaching, it's particularly effective, but we're going to use that function declaration style here to declare a very useful function.
[00:01:44]
10 squared, who is patient enough? Joe, Joe, help me out with how would I write the actual word by word for the function 10 squared on my board here, what would I write for the function 10 squared? Am I really going to do this? Yes, I am. Let's do function, yep. Space. Nice. 10 square. Yeah, I honestly, this is the best answer I've had. Closed in their parentheses, round ones, yep, and then a curly.
[00:02:17]
Yeah, I open it up and then I would just go right to the return in line it 10 by 10, 10 by 10. Beautiful. Thank you, Joe, excellent. Right, but I'm afraid, Joe, we have to declare another function right now. Oh no, there it is, 10 squared. OK, there it is, we call it, and it returns our 100, good. Excellent, there it is. There's our comment symbol. What about a 9 squared function, Joe? Ooh. Can you do this, Joe?
[00:02:54]
I don't know. Tell me, Joe, please. 9 squared. The problem is, Joe being so patient somewhat undermines my, let's do, let's do, let's do our, oh no, hallucinating autocompletes. Oh no. Yeah, exactly, let's. Joe, help me out here, and try and sound less generous because I want you to feel pained. Tell me exactly what we're doing again to declare 9 squared. We're going to return 9 x 9. OK. Oh, why am I?
[00:03:31]
Yeah, beautiful, return 9 by 9. I can already see why we might want to have a better answer, but before we get to any better answer. Well done, 9 squared. What about an 8 squared function? Joe, take it away. Function 8. No, OK, I can't take it, I can't take it anymore. What principle are we breaking, people? Do we know the principle we're breaking, they call it in programming? DRY, DRY, don't repeat yourself.
[00:04:01]
8 squared, 0, this, why have I then switched to 8 squared? We do NOT want to be breaking this principle. What could we do here, Joe, to save ourselves from this painful experience we just had? Make our squared function have a parameter. Right, and in the general sense, we could generalize our function. Exactly, we could make it reusable by leaving some of our data, the numbers we're applying our work to TBD.
[00:04:31]
What did you say allows us to do that? A parameter. Parameter, a placeholder that our function can then later fill in with actual data. We save it once, leave a blank, well, not a blank, a label, for where when we run the function, many times, we can reuse it with different data. We can generalize the function to make it reusable, square num, exactly as we heard from Joe. Call it, run it, invoke it later on with 10, then 9 with 8, and each time it's going to fill in that blank.
[00:05:01]
Generalizing functions parameters, they allow us to decide what data we run our functionality on when we run the function, when we use the function. We then provide the actual input, the argument when we run that function. What if we could not only leave some blank for our data, but also leave a TBD for some of our code. We save our function once, we then use it again and again and again with different data.
[00:05:34]
What if people, we could not only leave our function to be TBD on some of what the data it will be applied to, the numbers in this case, but also some of the functionality could be left up till later. That would allow us to have more generalizable, more reusable functions. And you know what people, I take this for granted, a little bit, I'm sure maybe we all do at this point, and I don't want to assume, but you know that in some of our favorite languages, the most prominent languages, Java, C++, this wasn't possible until the 2010s.
[00:06:09]
The idea of adjusting a function after it's been defined to give it different behaviors as opposed to just different data was not a built-in feature, you could kind of hack it, but was not a built-in feature for some of our key languages, added later. So, you know, don't take for granted the idea that we can have higher-order functions which we're going to see are where we decide some of our functionality only when we invoke or call the function.
Learn Straight from the Experts Who Shape the Modern Web
- 250+In-depth Courses
- Industry Leading Experts
- 24Learning Paths
- Live Interactive Workshops