摘要: Account = {balance = 0}function Account:withdraw(v) if v > self.balance then error"insufficient funds" end self.balance = self.balance - vendfunction Account:deposit(v) self.balance = self.balance + vendfunction Account:new(o) o = o or {} setmetatable(o, self) self.__index = self return 阅读全文
posted @ 2012-08-01 16:40 阿飞同学 阅读(748) 评论(0) 推荐(0) 编辑
摘要: 【1】closure就是一个函数加上该函数所需访问的所有的“非局部的变量”看下面一个例子:function newCounter() i = 0 return function() i = i + 1 return i endendc1 = newCounter()print(c1())print(c1())result:12用newCounter创建了一个函数,函数执行了(i=i+1 return i),调用一次c1,就执行一次该函数体,第一次打印出1。这个就是closure的作用,它保存了“i”这个非局部变量,每次只执行c1(),就使其加1。如果再有语句c2 = newC... 阅读全文
posted @ 2012-08-01 15:08 阿飞同学 阅读(641) 评论(0) 推荐(0) 编辑