Object-oriented Programming

  • Tables can be used to create classes in Lua
    • They can have name value pairs which act like variables in a class
    • They can reference functions by named indices which act like methods
    • They can reference themselves using the identifier "self" (a new concept)

The concept of self

Self refers to the present object (Tables are objects in Lua).  When the colon (:)
operator is used to call a function in the object/table self is passed into the
function (now referred to as a method) as a hidden variable.  Internally
in the function self can now be used to refer to an instance of a table.

This allow the functions to operate on different instances of tables giving
the ability to have object oriented programming (OOP).  Internally the functions
will use self to refer to their variables and functions

function Account:deposit(v)
   self.balance = self.balance + v --refers to a balance value within this table instance
end
```