Lua返回一個Closures函數(shù)實(shí)例
do
function Button(x)
print("Call Button");
x.action();
print(x.label);
end
function add_to_display(digit)
print("Call add_to_display");
print(digit);
end
function digitButton(digit)
return Button{//return a table and the function(Button), it means that the Button receives the param(the table{...})
label = tostring(digit),
action = function()
print("digit: ", digit);
add_to_display(digit);
end
}
end
local fun = digitButton(3);
end
寫個簡單的迭代器:
do
t_ = {9, 2, 3, 4};
function values(t)
local i = 0;
return function()
i = i + 1;
return t[i];
end
end
iter = values(t_);
while true do
local element = iter();
if element == nil then
break;
end
print(element);
end
end
相關(guān)文章
Lua的編譯、執(zhí)行和調(diào)試技術(shù)介紹
這篇文章主要介紹了Lua的編譯、執(zhí)行和調(diào)試技術(shù)介紹,本文著重講解了對錯誤的處理,另外也講解了編譯和執(zhí)行等知識,需要的朋友可以參考下2015-04-04Lua教程(四):在Lua中調(diào)用C語言、C++的函數(shù)
這篇文章主要介紹了Lua教程(四):在Lua中調(diào)用C語言、C++的函數(shù),本文給出了多個示例講解如何在Lua中調(diào)用C/C++寫的函數(shù),需要的朋友可以參考下2014-09-09Lua中的變量類型與語句學(xué)習(xí)總結(jié)
這篇文章主要介紹了Lua中的變量類型與語句學(xué)習(xí)總結(jié),總結(jié)了Lua入門過程中的一些基礎(chǔ)知識,需要的朋友可以參考下2016-06-06