Closures - Part 5 of Functional Programming in JavaScript

Fun Fun Function · Beginner ·🔧 Backend Engineering ·11y ago

Key Takeaways

This video explains the concept of closures in JavaScript, a fundamental concept in functional programming, using examples and code snippets to illustrate how closures work and their usefulness in tasks such as asynchronous callbacks.

Full Transcript

hello you whippers Snappers what are we talking about today I'll let to be excuses speak for that just finish the last video enjoy your vacation why thank you I did very much observe my my slightly more tan face and since you're asking for suggestions how about closures to be us closures it is in case you're a new viewer dropping by this is a series where we learn functional programming using JavaScript you can watch the other episodes in this series by clicking there so what are closures in Java Script functions are not just functions they are also closures what that means is that the function body has access to variables that are defined outside the function body let me show you have a look at this code we are assigning a variable here uh called me uh and we're assigning it the value Bruce Wayne and we are then declaring a function for greet me which console logs hello me exclamation mark and then we call the read me function here here let's run that hello Bruce Wayne notice that we are not passing in an argument here we're just calling it without any argument we are directly referring to the me that is assigned outside of the function scope the reason that we can do this is that JavaScript functions like greet me are closures we cannot do this in languages that does not have support for closures in a language without support for closures we would have to pass the name as an argument to the function instead because such a language does not have access to the uh the outer scope of itself like that let me go back to the original I really like to stress here that greet me does really have access to the outer variable scope it does not snapshot the value of me uh at the time it is declared for instance if I reassign me to be Batman here and run it it will say hello Batman so greet me does not copy the value of me it actually reads whatever the variable is at that time from the outter scope and it will do this even if greet me was an asynchronous function being called from a say an Ajax callback or something like that greet me will remember this outer context that it's declaring even if greet me is called from a completely different part of the application or a different module it will still refer to this specific context you all being healthy Skeptics you're probably now asking yourself why is this useful what is the point of closures closures actually have quite a bit of use cases but I'm going to give you one that I think illustrates why closures are useful let's look at this function together it's called send requests and we start by assigning a request ID to a variable we then use uh just a uh the standard jQuery ajax function we call an Earl and when that is successful we alert the string request request ID return return I'm not going to run this I'm going to let your imaginations execute it but what will happen is that since all functions in JavaScript or closures including the call back to success it will have access to the request ID that we declared even though this call back is executed way later this really illustrates a good use of closures it's when you start a task and you want to specify something that happens when that task is done with stuff that is available to you when you start the task closures makes that easy and readable in a language that does not support closures you would probably have to have some kind of data object that you pass into the ax function which it in turn passed into success and if it didn't have that you would probably have to create some kind of request object with properties and stuff instead of just simply doing a function this kind of thing becomes sort of icky if you don't have closures we have talked a bit about what closures are I have showed you a practical example of why they are a good thing but closures is one of those things in JavaScript that just permeates the entire language there are more use cases than the one I showed you more than will fit into this video or even a second one closures is just one of those things that you have to use a lot on your own in your code for you to really internalize it however Milla having just great job JavaScript documentation overall has a super super good page on closures on their site and I have linked it down in the show notes you should go there and check those examples out and start using closures in your code to get a feel for it and that is it for today as usual I'd love to hear from you either down below or at mpj me on Twitter tell me what you thought about this this episode or ask me a random question or maybe tell me what you think that the next episode Monday should be about and speaking of that episode make sure you don't miss it subscribe by clicking my face in the corner until next Monday stay curious and subscribe

Original Description

💖 Support the show by becoming a Patreon https://www.patreon.com/funfunfunction A short video explaining the concept of closures, using JavaScript. This is part of a series, where are learning functional programming using JavaScript. Mozilla JavaScript documentation on Closures https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures Code from the episode https://gist.github.com/mpj/b56371c672122a098d52 Playlist of full series https://www.youtube.com/playlist?list=PL0zVEGEvSaeEd9hlmCXrk5yUyqUag-n84 You want to follow me on Twitter and Quora: https://twitter.com/mpjme http://www.quora.com/Mattias-Petter-Johansson 💛 Follow on Twitch We record the show live Mondays 7 AM PT https://twitch.tv/funfunfunction 💛 Fun Fun Forum Private discussion forum with other viewers in between shows. https://www.funfunforum.com. Available to patron members, become one at https://www.patreon.com/funfunfunction 💛 mpj on Twitter https://twitter.com/mpjme 💛 CircleCI (Show sponsor) Robust and sleek Docker-based Continuous Integration as a service. I used CircleCI prior to them becoming a sponsor and I love that their free tier is powerful enough for small personal projects, even if they are private. Use this link when you sign up to let them know you came from here: https://circleci.funfunfunction.com 💛 Quokka (Show sponsor) Wonder how MPJ evaluates JavaScript inline his editor. Quokka is the answer - use this link when you buy to let them know you came from here: http://quokka.funfunfunction.com 💛 FUN FUN FUNCTION Since 2015, Fun Fun Function (FFF) is one of the longest running weekly YouTube shows on programming 🏅 thanks to its consistency and quality reaching 200,000+ developers. 🤦‍♂️ The Failing Together concept is what makes FFF unique. Most coding content out there focus on step-by-step tutorials. We think tutorials are too far removed from what everyday development is like. Instead, FFF has created a completely new learning environment where we grow from f
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Fun Fun Function · Fun Fun Function · 6 of 60

1 Higher-order functions - Part 1 of Functional Programming in JavaScript
Higher-order functions - Part 1 of Functional Programming in JavaScript
Fun Fun Function
2 Map - Part 2 of Functional Programming in JavaScript
Map - Part 2 of Functional Programming in JavaScript
Fun Fun Function
3 Reduce basics - Part 3 of Functional Programming in JavaScript
Reduce basics - Part 3 of Functional Programming in JavaScript
Fun Fun Function
4 Destructuring: What, Why and How - Part 1 of ES6 JavaScript Features
Destructuring: What, Why and How - Part 1 of ES6 JavaScript Features
Fun Fun Function
5 Reduce Advanced - Part 4 of Functional Programming in JavaScript
Reduce Advanced - Part 4 of Functional Programming in JavaScript
Fun Fun Function
Closures - Part 5 of Functional Programming in JavaScript
Closures - Part 5 of Functional Programming in JavaScript
Fun Fun Function
7 Too many tools and frameworks!
Too many tools and frameworks!
Fun Fun Function
8 Currying - Part 6 of Functional Programming in JavaScript
Currying - Part 6 of Functional Programming in JavaScript
Fun Fun Function
9 Recursion - Part 7 of Functional Programming in JavaScript
Recursion - Part 7 of Functional Programming in JavaScript
Fun Fun Function
10 Promises - Part 8 of Functional Programming in JavaScript
Promises - Part 8 of Functional Programming in JavaScript
Fun Fun Function
11 Staying relevant as a programmer
Staying relevant as a programmer
Fun Fun Function
12 Factory Functions in JavaScript
Factory Functions in JavaScript
Fun Fun Function
13 Composition over Inheritance
Composition over Inheritance
Fun Fun Function
14 Software needs to be better - FunFunFunction #1
Software needs to be better - FunFunFunction #1
Fun Fun Function
15 Unit testing: How to get your team started - FunFunFunction #2
Unit testing: How to get your team started - FunFunFunction #2
Fun Fun Function
16 Straight-line code over functions - FunFunFunction #3
Straight-line code over functions - FunFunFunction #3
Fun Fun Function
17 Clojure - FunFunFunction #5
Clojure - FunFunFunction #5
Fun Fun Function
18 The growth stages of a programmer - FunFunFunction #6
The growth stages of a programmer - FunFunFunction #6
Fun Fun Function
19 5 tips to quickly understand a new code base - FunFunFunction #7
5 tips to quickly understand a new code base - FunFunFunction #7
Fun Fun Function
20 Semicolons cannot save you! - FunFunFunction #9
Semicolons cannot save you! - FunFunFunction #9
Fun Fun Function
21 Functors - FunFunFunction #10
Functors - FunFunFunction #10
Fun Fun Function
22 Functors: I was WRONG! - FunFunFunction #11
Functors: I was WRONG! - FunFunFunction #11
Fun Fun Function
23 Questions and Answers - FunFunFunction #12
Questions and Answers - FunFunFunction #12
Fun Fun Function
24 Streams - FunFunFunction #13
Streams - FunFunFunction #13
Fun Fun Function
25 Prototypes in JavaScript - FunFunFunction #16
Prototypes in JavaScript - FunFunFunction #16
Fun Fun Function
26 Fast or Flexible? - FunFunFunction #17
Fast or Flexible? - FunFunFunction #17
Fun Fun Function
27 Coders are herd animals - FunFunFunction #18
Coders are herd animals - FunFunFunction #18
Fun Fun Function
28 Weekend Kubernetes Shenanigans - FunFunFunction #19
Weekend Kubernetes Shenanigans - FunFunFunction #19
Fun Fun Function
29 Monad - FunFunFunction #21
Monad - FunFunFunction #21
Fun Fun Function
30 Moar Weekend Shenanigans - FunFunFunction #23
Moar Weekend Shenanigans - FunFunFunction #23
Fun Fun Function
31 Questions and Answers - FunFunFunction #24
Questions and Answers - FunFunFunction #24
Fun Fun Function
32 Losing motivation - FunFunFunction #25
Losing motivation - FunFunFunction #25
Fun Fun Function
33 LONGEST KUBERNETES SHENANIGANS! - FunFunFunction #26
LONGEST KUBERNETES SHENANIGANS! - FunFunFunction #26
Fun Fun Function
34 Fast code is NOT important - FunFunFunction #27
Fast code is NOT important - FunFunFunction #27
Fun Fun Function
35 Pair Programming a Facebook Messenger Bot - FunFunFunction #28
Pair Programming a Facebook Messenger Bot - FunFunFunction #28
Fun Fun Function
36 Writing unit tests for personal projects? - FunFunFunction #29
Writing unit tests for personal projects? - FunFunFunction #29
Fun Fun Function
37 Let's Code a Pomodoro Button - FunFunFunction #30
Let's Code a Pomodoro Button - FunFunFunction #30
Fun Fun Function
38 What editor do you use? - FunFunFunction #31
What editor do you use? - FunFunFunction #31
Fun Fun Function
39 Arrow functions in JavaScript - What, Why and How - FunFunFunction #32
Arrow functions in JavaScript - What, Why and How - FunFunFunction #32
Fun Fun Function
40 Is Programming Art? - MPJ's Musings - FunFunFunction #33
Is Programming Art? - MPJ's Musings - FunFunFunction #33
Fun Fun Function
41 Generators in JavaScript - What, Why and How - FunFunFunction #34
Generators in JavaScript - What, Why and How - FunFunFunction #34
Fun Fun Function
42 Haskell Basics - FunFunFunction #35
Haskell Basics - FunFunFunction #35
Fun Fun Function
43 Haskell - Baby's first functions - FunFunFunction #36
Haskell - Baby's first functions - FunFunFunction #36
Fun Fun Function
44 Is Big O relevant to you? - Q&A Part 1 - FunFunFunction #37
Is Big O relevant to you? - Q&A Part 1 - FunFunFunction #37
Fun Fun Function
45 How much are you allowed to Google? - Q&A Part 2 - FunFunFunction #38
How much are you allowed to Google? - Q&A Part 2 - FunFunFunction #38
Fun Fun Function
46 Haskell lists - FunFunFunction #39
Haskell lists - FunFunFunction #39
Fun Fun Function
47 var, let and const - What, why and how - ES6 JavaScript Features
var, let and const - What, why and how - ES6 JavaScript Features
Fun Fun Function
48 Why are some programming languages popular? - MPJ's Musings  - FunFunFunction #41
Why are some programming languages popular? - MPJ's Musings - FunFunFunction #41
Fun Fun Function
49 Does a developer need to be nice? - MPJ's Musings - FunFunFunction #42
Does a developer need to be nice? - MPJ's Musings - FunFunFunction #42
Fun Fun Function
50 bind and this - Object Creation in JavaScript P1 - FunFunFunction #43
bind and this - Object Creation in JavaScript P1 - FunFunFunction #43
Fun Fun Function
51 Examples of this and bind - Object Creation in JavaScript P2 -  FunFunFunction #44
Examples of this and bind - Object Creation in JavaScript P2 - FunFunFunction #44
Fun Fun Function
52 Prototype basics - Object Creation in JavaScript P3 - FunFunFunction #46
Prototype basics - Object Creation in JavaScript P3 - FunFunFunction #46
Fun Fun Function
53 Separation of concerns RANT - MPJ's Musings - FunFunFunction #47
Separation of concerns RANT - MPJ's Musings - FunFunFunction #47
Fun Fun Function
54 Cellular Automata - Pair Programming - FunFunFunction #49
Cellular Automata - Pair Programming - FunFunFunction #49
Fun Fun Function
55 The 'new' keyword - Object Creation in JavaScript P4 - FunFunFunction #50
The 'new' keyword - Object Creation in JavaScript P4 - FunFunFunction #50
Fun Fun Function
56 __proto__ vs prototype - Object Creation in JavaScript P5 - FunFunFunction #52
__proto__ vs prototype - Object Creation in JavaScript P5 - FunFunFunction #52
Fun Fun Function
57 Unity game pair programming - Let's code - FunFunFunction #53
Unity game pair programming - Let's code - FunFunFunction #53
Fun Fun Function
58 Throw out your tools - MPJ's Musings - FunFunFunction #54
Throw out your tools - MPJ's Musings - FunFunFunction #54
Fun Fun Function
59 Unit tests vs. Integration tests - MPJ's Musings - FunFunFunction #55
Unit tests vs. Integration tests - MPJ's Musings - FunFunFunction #55
Fun Fun Function
60 Object.create - Object Creation in JavaScript P6 - FunFunFunction #57
Object.create - Object Creation in JavaScript P6 - FunFunFunction #57
Fun Fun Function

This video teaches the concept of closures in JavaScript, including how they work, their usefulness, and how to apply them in functional programming. Closures are essential in JavaScript and have many use cases, especially in asynchronous programming.

Key Takeaways
  1. Define a variable outside a function
  2. Declare a function that accesses the outer variable
  3. Call the function without passing the variable as an argument
  4. Understand how closures work in asynchronous callbacks
  5. Apply closures in functional programming tasks
💡 Closures in JavaScript have access to the outer variable scope, allowing them to remember the context even when called from a different part of the application or module.

Related Reads

Up next
Unlock CRAZY Performance: Native Code Compilation No JNI! #shorts #quarkusinsights #projectpanama
Quarkusio
Watch →