A JavaScript closure is a function that has a pointer reference to a free variable. A free variable is one that has fallen out of scope after its parent function has returned. However, if that outer function still has some reference to the free var (normally through a function that gets returned, or through a method property), the variable will not get garbage collected because it will have a non-zero reference count. Thus, from outside the function, we can still access the inner variable by means of the closure.
Copyright (c) 2013 Rodrigo Silveira http://www.easylearntutorial.com
Original source
44 responses to “JavaScript Closures 101: What is a closure?”
Brilliant and simple explanation. so closures in short words are functions that give access to private variables from the global scope. awesome stuff.
Awesome example and explanation. Thank you!
After being so impressed by this tutorial ,I went on checking his another tutorials,made me wonder why the hell he did'n make any other tutorial :/
Very Crisp explanation !!! Thanks a lot
Just wow! I was getting disappointed in myself after thinking about this for 4 hours and you made it clear in 4 minutes!
This vid was released in 2013 and you only have 2.4k subs??? Woah…something is fkd up with this world, because your explanation is so concise, so good, how can you be so unpopular?
This is the most incredible explanation of closures I've seen, and I've seen a few!
I watched this video twice and I every time fall asleep. Your voice. I will probably use this video when I have insomnia
Great explanation
Best I’ve seen it explained the oo route you took really helped drill the point because I’ve only ever used this.name instead of declaring a var thinking it was the same thing!
awesome..
Very clear and simple explanation!
One of the best example whatever i seen. Thank you Rodrigo superb… 🙂 less than in 5 min you explained. Thanks.
Beautiful! Short and sweet. I've seen other videos that are so confusing. Thank you Rodrigo. Good job 🙂
thanks for you explination!!! very clearly!
THANK YOU! The first time I ever saw someone name the PARAMETER and VARIABLE differently "just to make things clear"… which is very wise. Many docs and explanations seem to think they are being clever by using the same names.
MIND BLOWING! thank you mate!
fuck weird javascript
Neat
fantastic. explanation. Keep it up..
l come from c++. l been with javascript two years.
and l got to say javascript is great and easy to understand. l still studing and practice every day.
with javascript you can apply OOP. as you can see in this
Closure: a function remembers its lexical scope even when it is executed outside that lexical scope.
How would I apply a closure in real world? What is its purpose?
This is the best/clear/short explanation of what is a closure! Thank you!
Great! Thanks!
Wonderful Sir
Thanks a lot, this was really helpful.
Rodrigo make more Videos dude!!.. You have 2K subscribers with only one video.. LOL Killer.
hey man what distro is this? nice setup
I watched this video because I was trying to understand the idea of anonymous classes having closure in Java (not JavaScript) and I thought hat the concepts might translate.
This doesn't seem different from a standard 'getter' function to expose private fields in a class. If the excitement that the getter is attached to a function, not to a class? I've seen JavaScript patterns like Revealing Module Pattern to simulate OOP in JavaScript except using functions instead of classes.
can anybody tell me when i need to use closures…? and give me real world example where closure exist..?
can anybody tell me when i need to use closures…? and give me real world example where closure exist..?
That's pretty cool explanation, to the point and very clear. Keep it up Rodrigo. and Thank you );
Awesome explanation! I finally understand this. What would be a case where we need to return the inner function?
I don't undersand why I got different in the final example .I type these in my chrome
function Person(name) {
var _name = name;
this.getName = function(){
return _name;
};
}
var me = new Person("Mihai");
but I got Person{};without name;
cool.
awesome explanation.. Thank you!
🙂 🙂 🙂
thank you so much
super clear genius
Great explanation! Thank you!
Waw in 4 min what others didn't explain in more than 20 min and long articles, so clear man thank you.
for once, i’ve understood what closures are.
thanks a lot