我一直看到两种在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
我无法完全理解两者之间的行为差异。有人可以启发我吗?