Can a Function Be a Closure and a Callback?

John Lindquist
InstructorJohn Lindquist
Share this video with your friends

Social Share Links

Send Tweet
Published 4 years ago
Updated 4 years ago

The short answer: "yes". Functions have a lot of names: predicates, lambdas, reducers, etc. It all depends on the usage of the function or if its inputs/outputs match certain rules. This lesson shows how closures and callbacks are both types of functions.

John Lindquist: [0:00] We can combine the powers of a callback and a closure simply by defining something outside the callback. I want to say a . We'll access that inside of here, so i++. Now when I hit Save, this function serves as both a callback because it's an argument to another function, and a closure because it's capturing this i, which is defined outside of it.

[0:25] You can see this another function, which is calling it three times, is logging out , 1, and 2. Similarly, if we delete this, bring back our document, addEventListener. We'll say clickAndCallback, hit Save. Every time I click, click, click, click, click, you'll see it's counting up each time I click.