lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

On Tue, Apr 23, 2013 at 4:53 PM, xinlei fan <> wrote:
>     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 [...]

No, that code doesn't create a cycle unless you call


Because it would set Account as its own prototype. Here's the minimal code
to create a prototype loop:

    t.__index = t

In the PiL example, self becomes the metatable of the new object, not its own.

-- Pierre-Yves