Created
September 9, 2015 12:43
-
-
Save noname007/66e3250fde2e40392a40 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
p = print | |
local mt ={} | |
local _t = {} | |
local mt_t = {} | |
mt.__newindex = _t | |
mt_t.__newindex = function(t,k,v) | |
print(t,k,v) | |
end | |
setmetatable(_t, mt_t) | |
local a = {1,2,3,4,6} | |
setmetatable(a, mt) | |
p('a ',a) | |
p('_t',_t) | |
a['string_key'] = 10 | |
a[2] = 100 | |
p (table.concat( a, ", " )) | |
--[[ | |
输出结果: | |
------------------- | |
验证了: | |
@1.__newindex 如果为一个表,当调用这个则直接调用这张表的索引 | |
@2.每个table都有不同的元表 | |
------------------- | |
a table: 00000000003c9bd0 | |
_t table: 00000000003c9b50 | |
table: 00000000003c9b50 string_key 10 | |
1, 100, 3, 4, 6 | |
--]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment