mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-12-23 17:41:41 +00:00
Merge 9867253a28
into 0b375fe86c
This commit is contained in:
commit
52ca46360f
@ -232,8 +232,8 @@ eatenBy = myFavs.animal -- works! thanks, metatable
|
|||||||
-- An __index value can also be a function(tbl, key)
|
-- An __index value can also be a function(tbl, key)
|
||||||
-- for more customized lookups.
|
-- for more customized lookups.
|
||||||
|
|
||||||
-- Values of __index,add, .. are called metamethods.
|
-- The values of __index, __add, etc are called
|
||||||
-- Full list. Here a is a table with the metamethod.
|
-- metamethods. Here's their full list:
|
||||||
|
|
||||||
-- __add(a, b) for a + b
|
-- __add(a, b) for a + b
|
||||||
-- __sub(a, b) for a - b
|
-- __sub(a, b) for a - b
|
||||||
@ -258,7 +258,7 @@ eatenBy = myFavs.animal -- works! thanks, metatable
|
|||||||
-- Classes aren't built in; there are different ways
|
-- Classes aren't built in; there are different ways
|
||||||
-- to make them using tables and metatables.
|
-- to make them using tables and metatables.
|
||||||
|
|
||||||
-- Explanation for this example is below it.
|
-- The explanation for this example follows it.
|
||||||
|
|
||||||
Dog = {} -- 1.
|
Dog = {} -- 1.
|
||||||
|
|
||||||
@ -332,9 +332,6 @@ end
|
|||||||
|
|
||||||
--[[ I'm commenting out this section so the rest of
|
--[[ I'm commenting out this section so the rest of
|
||||||
-- this script remains runnable.
|
-- this script remains runnable.
|
||||||
```
|
|
||||||
|
|
||||||
```lua
|
|
||||||
-- Suppose the file mod.lua looks like this:
|
-- Suppose the file mod.lua looks like this:
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
@ -369,7 +366,13 @@ mod.sayMyName() -- error
|
|||||||
-- require's return values are cached so a file is
|
-- require's return values are cached so a file is
|
||||||
-- run at most once, even when require'd many times.
|
-- run at most once, even when require'd many times.
|
||||||
|
|
||||||
-- Suppose mod2.lua contains "print('Hi!')".
|
-- Suppose another file, mod2.lua, containing this:
|
||||||
|
print('Hi!')
|
||||||
|
return function ()
|
||||||
|
return 'foo'
|
||||||
|
end
|
||||||
|
|
||||||
|
--
|
||||||
local a = require('mod2') -- Prints Hi!
|
local a = require('mod2') -- Prints Hi!
|
||||||
local b = require('mod2') -- Doesn't print; a=b.
|
local b = require('mod2') -- Doesn't print; a=b.
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user