Link: https://www.ibm.com/developerworks/cn/linux/l-cn-closure/
1. Built-in operating environment, easy to call
example:
//Define a function that loops 10 times function do10times(fn) for i = 0,9 do fn (i) end end //define an accumulating function sum = 0 function addsum(i) sum = sum + i end //accumulate from 0 to 9 do10times(addsum) print(sum)
, the function addsum is passed to the function do10times, and is called 10 times in do10times. It is not difficult to see that the actual execution point of addsum is inside do10times, it wants to access the non-local variable sum, and do10times is not in the scope of sum. This also doesn't seem to work properly. The closure feature can break through this limitation. A closure is a piece of code with its own running environment.