lua-users home
lua-l archive

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


On Sun, Nov 22, 2015 at 2:30 PM, Andreas Matthias
<andreas.matthias@gmail.com> wrote:
> Why is
> `self.count' a class variable in the following example while `self.text'
> is a member variable?

Because in Display:new(), "self" refers to the class, not the instance
you created with "local me = {}". Thus when assigning to "self.count"
in the "new" method, you are assigning directly to the class rather
than to the newly created instance. If you want it in an instance
variable, then in the "new" method you need to perform the assignment
on "me" rather than "self".

Also, the "self.__index = self" is technically correct, but as it's
going to do the exact same thing every time an instance is created, it
would be more efficient to drop it from the "new" method and put it at
the top with the creation of "Display", as "Display,__index  =
Display", so that it only runs once.