Deprecated: Function create_function() is deprecated in /var/www/vhosts/eduardoarellano.com/d4mations.com/wp-content/plugins/wp-automatic/wp-automatic.php on line 70
JavaScript Closures 101: What is a closure? | D4mations.com
  • Tuesday , 29 September 2020

JavaScript Closures 101: What is a closure?

Code Canyon



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

3d Ocean

Related Posts

44 Comments

  1. mo swilam
    January 31, 2019 at 14:23

    Brilliant and simple explanation. so closures in short words are functions that give access to private variables from the global scope. awesome stuff.

  2. Gail Parsloe
    January 31, 2019 at 14:23

    Awesome example and explanation. Thank you!

  3. Shaheryar ahmed
    January 31, 2019 at 14:23

    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 :/

  4. Amit Meena
    January 31, 2019 at 14:23

    Very Crisp explanation !!! Thanks a lot

  5. Hooman st
    January 31, 2019 at 14:23

    Just wow! I was getting disappointed in myself after thinking about this for 4 hours and you made it clear in 4 minutes!

  6. Solomon Jenkins
    January 31, 2019 at 14:23

    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?

  7. Solomon Jenkins
    January 31, 2019 at 14:23

    This is the most incredible explanation of closures I've seen, and I've seen a few!

  8. Emad El Sammad
    January 31, 2019 at 14:23

    I watched this video twice and I every time fall asleep. Your voice. I will probably use this video when I have insomnia

  9. Brett Walker
    January 31, 2019 at 14:23

    Great explanation

  10. Bradley Turner
    January 31, 2019 at 14:23

    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!

  11. robin stark
    January 31, 2019 at 14:23

    awesome..

  12. Praxis Dev1
    January 31, 2019 at 14:23

    Very clear and simple explanation!

  13. Rakesh Shah
    January 31, 2019 at 14:23

    One of the best example whatever i seen. Thank you Rodrigo superb… 🙂 less than in 5 min you explained. Thanks.

  14. Kevork Yerevanian
    January 31, 2019 at 14:23

    Beautiful! Short and sweet. I've seen other videos that are so confusing. Thank you Rodrigo. Good job 🙂

  15. Edgar Britez
    January 31, 2019 at 14:23

    thanks for you explination!!! very clearly!

  16. blarvinius
    January 31, 2019 at 14:23

    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.

  17. Andrei Costin
    January 31, 2019 at 14:23

    MIND BLOWING! thank you mate!

  18. Ankit Singh
    January 31, 2019 at 14:23

    fuck weird javascript

  19. Aka Poolka
    January 31, 2019 at 14:23

    Neat

  20. Mohan J
    January 31, 2019 at 14:23

    fantastic. explanation. Keep it up..

  21. Giancarlo Andre Bravo Abanto
    January 31, 2019 at 14:23

    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

  22. Do Hugues
    January 31, 2019 at 14:23

    Closure: a function remembers its lexical scope even when it is executed outside that lexical scope.

  23. Robert Williams
    January 31, 2019 at 14:23

    How would I apply a closure in real world? What is its purpose?

  24. Combine
    January 31, 2019 at 14:23

    This is the best/clear/short explanation of what is a closure! Thank you!

  25. Ben Marley
    January 31, 2019 at 14:23

    Great! Thanks!

  26. mohit saraswat
    January 31, 2019 at 14:23

    Wonderful Sir

  27. Nick Fletcher
    January 31, 2019 at 14:23

    Thanks a lot, this was really helpful.

  28. Rolando Niubó
    January 31, 2019 at 14:23

    Rodrigo make more Videos dude!!.. You have 2K subscribers with only one video.. LOL Killer.

  29. Tristan
    January 31, 2019 at 14:23

    hey man what distro is this? nice setup

  30. CamdenBloke
    January 31, 2019 at 14:23

    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.

  31. Hanamant kj
    January 31, 2019 at 14:23

    can anybody tell me when i need to use closures…? and give me real world example where closure exist..?

  32. Hanamant kj
    January 31, 2019 at 14:23

    can anybody tell me when i need to use closures…? and give me real world example where closure exist..?

  33. Kashif Hussain
    January 31, 2019 at 14:23

    That's pretty cool explanation, to the point and very clear. Keep it up Rodrigo. and Thank you );

  34. Jada H
    January 31, 2019 at 14:23

    Awesome explanation! I finally understand this. What would be a case where we need to return the inner function?

  35. Mihai Lai
    January 31, 2019 at 14:23

    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;

  36. Amador Designs
    January 31, 2019 at 14:23

    cool.

  37. Uma Sankar Bugatha
    January 31, 2019 at 14:23

    awesome explanation.. Thank you!

  38. puneeth shivalingaiah
    January 31, 2019 at 14:23

    🙂 🙂 🙂

  39. SaLLaK LamHayyen
    January 31, 2019 at 14:23

    thank you so much

  40. David Dolynny
    January 31, 2019 at 14:23

    super clear genius

  41. bem b
    January 31, 2019 at 14:23

    Great explanation! Thank you!

  42. Abdou Rakdesign
    January 31, 2019 at 14:23

    Waw in 4 min what others didn't explain in more than 20 min and long articles, so clear man thank you.

  43. Vani Ananthuni
    January 31, 2019 at 14:23

    for once, i’ve understood what closures are.

  44. Wandering Backpacker
    January 31, 2019 at 14:23

    thanks a lot

Leave A Comment

You must be logged in to post a comment.