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 3 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.

egghead
egghead
~ 46 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today