mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-12-25 08:13:29 +03:00
Merge pull request #306 from hoelzro/master
A few clarifications for the Lua documentation
This commit is contained in:
commit
bc12c626c8
@ -125,6 +125,9 @@ f = function (x) return x * x end
|
|||||||
|
|
||||||
-- And so are these:
|
-- And so are these:
|
||||||
local function g(x) return math.sin(x) end
|
local function g(x) return math.sin(x) end
|
||||||
|
local g = function(x) return math.xin(x) end
|
||||||
|
-- Equivalent to local function g(x)..., except referring
|
||||||
|
-- to g in the function body won't work as expected.
|
||||||
local g; g = function (x) return math.sin(x) end
|
local g; g = function (x) return math.sin(x) end
|
||||||
-- the 'local g' decl makes g-self-references ok.
|
-- the 'local g' decl makes g-self-references ok.
|
||||||
|
|
||||||
@ -133,6 +136,10 @@ local g; g = function (x) return math.sin(x) end
|
|||||||
-- Calls with one string param don't need parens:
|
-- Calls with one string param don't need parens:
|
||||||
print 'hello' -- Works fine.
|
print 'hello' -- Works fine.
|
||||||
|
|
||||||
|
-- Calls with one table param don't need parens
|
||||||
|
-- either (more on tables below):
|
||||||
|
print {} -- Works fine too.
|
||||||
|
|
||||||
|
|
||||||
----------------------------------------------------
|
----------------------------------------------------
|
||||||
-- 3. Tables.
|
-- 3. Tables.
|
||||||
@ -203,7 +210,7 @@ f2 = {a = 2, b = 3}
|
|||||||
|
|
||||||
metafraction = {}
|
metafraction = {}
|
||||||
function metafraction.__add(f1, f2)
|
function metafraction.__add(f1, f2)
|
||||||
sum = {}
|
local sum = {}
|
||||||
sum.b = f1.b * f2.b
|
sum.b = f1.b * f2.b
|
||||||
sum.a = f1.a * f2.b + f2.a * f1.b
|
sum.a = f1.a * f2.b + f2.a * f1.b
|
||||||
return sum
|
return sum
|
||||||
@ -266,7 +273,7 @@ eatenBy = myFavs.animal -- works! thanks, metatable
|
|||||||
Dog = {} -- 1.
|
Dog = {} -- 1.
|
||||||
|
|
||||||
function Dog:new() -- 2.
|
function Dog:new() -- 2.
|
||||||
newObj = {sound = 'woof'} -- 3.
|
local newObj = {sound = 'woof'} -- 3.
|
||||||
self.__index = self -- 4.
|
self.__index = self -- 4.
|
||||||
return setmetatable(newObj, self) -- 5.
|
return setmetatable(newObj, self) -- 5.
|
||||||
end
|
end
|
||||||
@ -301,7 +308,7 @@ mrDog:makeSound() -- 'I say woof' -- 8.
|
|||||||
LoudDog = Dog:new() -- 1.
|
LoudDog = Dog:new() -- 1.
|
||||||
|
|
||||||
function LoudDog:makeSound()
|
function LoudDog:makeSound()
|
||||||
s = self.sound .. ' ' -- 2.
|
local s = self.sound .. ' ' -- 2.
|
||||||
print(s .. s .. s)
|
print(s .. s .. s)
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -322,7 +329,7 @@ seymour:makeSound() -- 'woof woof woof' -- 4.
|
|||||||
|
|
||||||
-- If needed, a subclass's new() is like the base's:
|
-- If needed, a subclass's new() is like the base's:
|
||||||
function LoudDog:new()
|
function LoudDog:new()
|
||||||
newObj = {}
|
local newObj = {}
|
||||||
-- set up newObj
|
-- set up newObj
|
||||||
self.__index = self
|
self.__index = self
|
||||||
return setmetatable(newObj, self)
|
return setmetatable(newObj, self)
|
||||||
|
Loading…
Reference in New Issue
Block a user