我不明白这里的语法
I dont understand the syntax here
我在 roblox devforum 上遇到了一个 post 关于 metatable 的问题,在代理 table 部分我不明白这段代码的语法
local function TrackDownTable()
local t = {x = 5}
local proxy = setmetatable({}, {
__index = function(_, key)
print("User indexed table with "..key)
return t[key]
end,
__newindex = function(_, key, value)
print("User made or update the field "..key.." with the value "..value.." in table")
t[key] = value
end
})
return proxy
end
local t = TrackDownTable()
t.x = 5
print(t.x)
这部分
local t = TrackDownTable()
t.x = 5
local t = TrackdownTable()
是做什么的?这部分 t.x = 5
如何访问 proxy
table?
这是一个非常简单的代理演示 table。
代理 table 是控制 table 访问的 table。例如,您可以使用它来跟踪 table 访问或实现只读 table。
如果不通过代理 table,您将无法访问或修改 table 的数据。
其实很简单。 TrackDownTable 创建 t
,这只是一些演示 table。我们只需要一个简单的 table 来演示 table 访问。所以我们创建一个最小的 table 和一个字段 {x=5}
local proxy = setmetatable({}, {
__index = function(_, key)
print("User indexed table with "..key)
return t[key]
end,
__newindex = function(_, key, value)
print("User made or update the field "..key.." with the value "..value.." in table")
t[key] = value
end
})
可以改写为:
local metatable = {}
metatable.__index = function(_, key)
print("User indexed table with "..key)
return t[key]
end
metatable.__newindex = function(_, key, value)
print("User made or update the field "..key.." with the value "..value.." in table")
t[key] = value
end
local proxy = {}
setmetatable(proxy, metatable)
此代码简单地创建了一个带有 __index
和 __newindex
元方法的元 table,并将其设置为我们的演示 table 的元 table .
__index
索引代理字段时调用
__newindex
在代理中为索引赋值时调用。
编辑:
want to know is how is this assignmentt.x = 5 passed to the proxy
table as ``local t = TrackDownTable() ``` when t.x = 5 happens what
does it do, it passes 5 as the parameter to the function?
t.x = 5
是一个索引赋值。如果执行此 Lua 将检查 t
中是否有键为 "x"
的字段。由于 t["x"]
在此范围内为 nil,它将检查是否存在 metatable。有,所以它会调用我们的 __newindex
元方法,它有 3 个参数 (table, key, value)
所以我们实际上调用了 getmetatable(t).__index(t, "x", 5)
,它在内部将值分配给在 TrackDownTable 中定义的本地 t.x
。
在这个例子中两个 table 都被命名为 t
有点误导。
我在 roblox devforum 上遇到了一个 post 关于 metatable 的问题,在代理 table 部分我不明白这段代码的语法
local function TrackDownTable()
local t = {x = 5}
local proxy = setmetatable({}, {
__index = function(_, key)
print("User indexed table with "..key)
return t[key]
end,
__newindex = function(_, key, value)
print("User made or update the field "..key.." with the value "..value.." in table")
t[key] = value
end
})
return proxy
end
local t = TrackDownTable()
t.x = 5
print(t.x)
这部分
local t = TrackDownTable()
t.x = 5
local t = TrackdownTable()
是做什么的?这部分 t.x = 5
如何访问 proxy
table?
这是一个非常简单的代理演示 table。
代理 table 是控制 table 访问的 table。例如,您可以使用它来跟踪 table 访问或实现只读 table。
如果不通过代理 table,您将无法访问或修改 table 的数据。
其实很简单。 TrackDownTable 创建 t
,这只是一些演示 table。我们只需要一个简单的 table 来演示 table 访问。所以我们创建一个最小的 table 和一个字段 {x=5}
local proxy = setmetatable({}, {
__index = function(_, key)
print("User indexed table with "..key)
return t[key]
end,
__newindex = function(_, key, value)
print("User made or update the field "..key.." with the value "..value.." in table")
t[key] = value
end
})
可以改写为:
local metatable = {}
metatable.__index = function(_, key)
print("User indexed table with "..key)
return t[key]
end
metatable.__newindex = function(_, key, value)
print("User made or update the field "..key.." with the value "..value.." in table")
t[key] = value
end
local proxy = {}
setmetatable(proxy, metatable)
此代码简单地创建了一个带有 __index
和 __newindex
元方法的元 table,并将其设置为我们的演示 table 的元 table .
__index
索引代理字段时调用
__newindex
在代理中为索引赋值时调用。
编辑:
want to know is how is this assignmentt.x = 5 passed to the proxy table as ``local t = TrackDownTable() ``` when t.x = 5 happens what does it do, it passes 5 as the parameter to the function?
t.x = 5
是一个索引赋值。如果执行此 Lua 将检查 t
中是否有键为 "x"
的字段。由于 t["x"]
在此范围内为 nil,它将检查是否存在 metatable。有,所以它会调用我们的 __newindex
元方法,它有 3 个参数 (table, key, value)
所以我们实际上调用了 getmetatable(t).__index(t, "x", 5)
,它在内部将值分配给在 TrackDownTable 中定义的本地 t.x
。
在这个例子中两个 table 都被命名为 t
有点误导。