lua 編程問題

lua IO

io.output("xxx1")  -- also be writed: local ff1 = io.output("xxx1"); 實際是以"w"模式打開文件xxx1,只是把句柄保存到全局表裏

io.write("*")
io.close()  -- equal to [io.close(io.output())] and [io.output():close()]


io.input("xxx2")  -- also be writed: local ff2 = io.output("xxx2"); 實際是以"r"模式打開文件xxx2,只是把句柄保存到全局表裏
io.read("*")
io.close(io.input())  -- io.input():close()


local f1 = io.open("xxx3", "w")

f1:close()


lua variable scope

lua 的變量 經過dofile require執行後,在外層依然可見;反之,外層變量在dofile和require裏也是可見的

改變函數的環境

aa = 1
bb = 2
function nn()
        local _ENV = {["aa"] = 100, ["print"]=_ENV.print}
        return function () --   此函數的環境變量變爲   { ["aa"] = 100, ["print"]=_ENV.print }

                print(aa) -- 100
                print(bb) -- nil
                end
end

local ff = nn()
ff()




發佈了63 篇原創文章 · 獲贊 11 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章