1

Do you know any real life example of closures ??.. where I can use them in real world .

Comments
  • 0
    What language would this pertrain to? Js?
  • 0
    @AleCx04 in Js.. means why do we need to use something like closure
  • 2
    @MrZeroCool
    Well in JS they are used as a way to encapsulate variables.
    So for example if we have

    function counter (){
    var count = 0;
    return function inside(){
    count++;
    return count;
    }
    }

    We have a closure. Closures only happen when the inner function uses a variable defined in the scope of the parent. To make use of this

    var counter_test = counter ();

    What we have as the return value is inner(), upon execution we will see the following:
    counter_test () will give out one
    counter_test () will now give out two.

    Considering how this may be useful in the real world. We can think of the use of data that you passed into the counter function through a parameter that will be used again for some other execution state.
  • 0
    @AleCx04 pretty convincing
Add Comment