So, the below is valid -
We call point a variable
getProductPlus, and pass arguments.
Then we execute
prod(). In other languages this likely implies that the outer function has been executed and has already lost its variables.
Closure works because -
- Reference is maintained to the context until the evaluation is complete. Variables retain values until inner function is evaluated
Here, the program remembers the values when we initialize
cube. We can then use the initiated variables as functions to get the final result.
You might have seen by now that closures have some degree of similarity with
objects (as in instances of
classes) and methods of objects that retain data specific to objects.