Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
|
|
@ -0,0 +1,4 @@
|
|||
local object = { name = "foo", func = function (self) print(self.name) end }
|
||||
|
||||
object:func() -- with : sugar
|
||||
object.func(object) -- without : sugar
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
local methods = { }
|
||||
function methods:func () -- if a function is declared using :, it is given an implicit 'self' parameter
|
||||
print(self.name)
|
||||
end
|
||||
|
||||
local object = setmetatable({ name = "foo" }, { __index = methods })
|
||||
|
||||
object:func() -- with : sugar
|
||||
methods.func(object) -- without : sugar
|
||||
16
Task/Call-an-object-method/Lua/call-an-object-method-3.lua
Normal file
16
Task/Call-an-object-method/Lua/call-an-object-method-3.lua
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
local count = 0
|
||||
local box = { }
|
||||
local boxmt = { __index = box }
|
||||
function box:tellSecret ()
|
||||
return self.secret
|
||||
end
|
||||
|
||||
local M = { }
|
||||
function M.new ()
|
||||
count = count + 1
|
||||
return setmetatable({ secret = count, contents = count % 2 == 0 and "rabbit" or "rock" }, boxmt)
|
||||
end
|
||||
function M.count ()
|
||||
return count
|
||||
end
|
||||
return M
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
local box = require 'box'
|
||||
|
||||
local b = box.new()
|
||||
|
||||
print(b:tellSecret())
|
||||
print(box.count())
|
||||
Loading…
Add table
Add a link
Reference in a new issue