What are closures?
I consider closures in JavaScript as an advanced topic. It is one of the topics that gets asked in interviews a lot.
...
For further actions, you may consider blocking this person and/or reporting abuse
Awesome post, Parwinder!
One clarification that I think is worth mentioning. My understanding is that the variable declared and initialized is not GC'd because there's a child function. I think it's actually simpler than that, right? It isn't GC'd so long as ANYTHING maintains a reference to it, whether it's a function, another variable, some object's state, etc
Absolutely love the last example! I feel like people don't really know this and this is ideally how encapsulation is done. However, it may be worth mentioning that it's usefulness or utility comes from being able to enforce an interface -- forcing the consumer of the API to use the methods defined on the class instead of accessing properties/fields that aren't supposed to be accessible just because they can...
David, great feedback β€οΈ
The more I write, the better writer I am becoming (or at least I think I am).
Then I see feedback like yours, and I am surprised that I could have presented things in a lot better way. I will update the post to reflect your feedback!
You're doing great. I always miss a lot of things and love learning about what I've forgotten or don't yet know (there's a lot) :D
When I used to work in JavaScript and interview folks, I typically asked this question that is also solved with the use of a closure:
Haha, I am glad that you have mentioned this example. This is a go-to question for me too.
Hi, great article!
I really liked your last example and decided to code it myself. After executing it, I got a bunch of sixes, as expected. However, when I declared the variable i using the βletβ keyword, I got 0-5. Why is that happening? I thought i has a value of 6 at the time of invocation.
Thanks for reading and bringing up the confusion. This happened due to my copy paste job from my notes. I have corrected it to
var
.As a bonus, I have updated the blog post with an explanation of why
let
solves this problem as well (similar to closure + IIFE). This should answer the question you have π If you still have any more questions please feel free to follow up.Thank you for the quick response! Your explanation answered my question π
I see lots of similarities between closures and classes. Here is another car speed example, but using classes. I'm not saying that the two are completely interchangeable for all things, but I'd like to know where it's better to leverage closures instead of classes, or even, employ a mixture of both techniques together?
Ok, I had an idea where we could employ a closure to break past the "Max" speed of a given vehicle via a logarithmic curve. ... It's probably best that I step away from the keyboard...
Sir I think the example you have given for "for loop using let" will give correct answer as 0,1,... because "let is block scope" and we don't want to use IIFE at that time but if we use "var " there in for then actually we need IIFE so that we can get 0,1,... as answer bcz var is function scoped.
Thanks for bringing it up. This confusion happened due to my copy paste job from my notes. I have corrected it to
var
. As a bonus, I have updated the blog post with explanation on whylet
solves this problem as well (similar to closure + IIFE).Awesome part of JS for interviews, and also useless part of real life apps. There are better, much clever, and simpler solutions. I think. ;)
Thanks for the feedback. Clever examples are not always easy to understand. My aim is to make it easy for a beginner to grasp the idea of encapsulation and interface implementation.
That being said, I would welcome any examples you might have that are easy and cleaver. ππ½
Thanks a lot.. And finally i understand closures.. :P