Class Declaration Statements
In the body of a class declaration, we can have normal expressions in addition to key/value pairs. In this context, self is equal to the class object.
Here is an alternative way to create a class variable compared to what’s described above:
MoonScript:
class Things
@class_var = "hello world"
Lua:
local Things
do
local _base_0 = { }
_base_0.__index = _base_0
local _class_0 = setmetatable({
__init = function() end,
__base = _base_0,
__name = "Things"
}, {
__index = _base_0,
__call = function(cls, ...)
local _self_0 = setmetatable({}, _base_0)
cls.__init(_self_0, ...)
return _self_0
end
})
_base_0.__class = _class_0
local self = _class_0
self.class_var = "hello world"
Things = _class_0
end
These expressions are executed after all the properties have been added to the base.
All variables declared in the body of the class are local to the classes properties. This is convenient for placing private values or helper functions that only the class methods can access:
MoonScript:
class MoreThings
secret = 123
log = (msg) -> print "LOG:", msg
some_method: =>
log "hello world: " .. secret