Reading time: min
Which from the memory view point, means that in case of a normal function execution, the arguments of the function are stored in Stack Memory(short lived) and deleted once the function is has fulfilled the job!
But in closure, The use of global var, leads the app to use the Heap Memory(long lived), beside the Stack Memory for arguments, so the closure uses more memory!
But there is a reason that such a thing exists and is popular, right?
The Closure, also causes Data Encapsulation, prevent leaking or exposing data where it’s nor needed. In order to use this feature of closure, you can create an outer function, with a variable inside it, and then create another function inside the primary function and return the function for example.
So what happens here, is that the inner function can access the variables out side of itself, which is inside the outer function, but the outer function, can not use the data inside the inner function, so the data in memory is created, and vanished already
You can also watch this video to have a better understanding
Was this page helpful?
What was the most helpful point of this page for you?
Thanks for your cooperation!