Lua 元表 __index 位置的差异

我经常看到两种在元表上定义 __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

我无法完全理解两者之间的行为差异是什么。有人可以给我解释一下吗?

点赞
用户2505965
用户2505965

区别在于创建的表数量和创建的表链。

在第一个例子中,Account表既充当所有实例的共享元表,又作为“__index”元方法的查找目标。创建如下链:

instance -> Account, __index -> Account

在第二个例子中,从create方法返回的每个实例都有自己的唯一元表,该元表充当实例与“类”之间的桥梁。创建的链:

instance -> (匿名的,唯一的表), __index -> Account

有时你也会看到表充当自己的元表:

Account = {}

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

它创建了以下链:

instance -> instance, __index -> Account

第一种和第三种风格的优点是创建的表数量较少,可以简化某些实现,并减少内存占用。第二种风格可以说更加健壮,因为每个实例都有自己的元表,可以单独操纵。

你使用的风格取决于你的程序要求以及你对实现任何给定风格的熟练程度。

2016-08-16 11:30:21