Lua metatable __index定位的差异

时间:2016-08-16 11:11:28

标签: inheritance lua prototype metatable

我一直看到两种在metatable上定义__index的方法:

Account = {}
Account.__index = Account

function Account.create(balance)
   local self = { balance = balance }
   return setmetatable(self, Account)
end

或者:

Account = {}

function Account.create(balance)
   local self = { balance = balance }
   return setmetatable(self, { __index = Account })
end

我无法完全理解两者之间的行为差​​异。有人可以启发我吗?

0 个答案:

没有答案