【发布时间】:2020-09-23 15:34:04
【问题描述】:
根据指南(https://www.tutorialspoint.com/lua/lua_quick_guide.htm), 上面写着:
array = {"Lua", "Tutorial"}
function elementIterator (collection)
local index = 0
local count = #collection
-- The closure function is returned
return function ()
index = index + 1
if index <= count
then
-- return the current element of the iterator
return collection[index]
end
end
end
for element in elementIterator(array)
do
print(element)
end
闭包函数对 Lua 意味着什么?
【问题讨论】:
-
这能回答你的问题吗? What is a 'Closure'?
-
闭包是一个使用其父函数的局部变量的函数。例如,您的示例中返回的函数取决于变量
index、count和collection。
标签: lua