I am a freshman in lua programming,and I worked with _javascript_ for several years
function Account:new (o)
o = o or {} -- create object if user does not provide one
setmetatable(o, self)
self.__index = self
return o
end
I think "self.__index = self" is not make sense,it will lead to a "prototype circle",
because when you call Account:new({}) after, the table Account will have a
prototype point to himself,and if you dump Account (loop each key in it,if is a table,loo
p the table and so on,in recursive),you will get an stack overflow error..... And in logic
when you create a "instance" of a "Class",you shouldn't change the "Class's" property or behavior
I think it should code like this
function Account:new (o)
o = o or {} -- create object if user does not provide one
setmetatable(o, {__index = self})
return o
end
It's much easy to understand,and when you create instance of Account,you will not change the
Account's any definition,and the table o(instance) also have a prototype point to Account(Class),it can call the method of the Account(Class)。