Object-Oriented Programming in JavaScript and Lua
Even though some may perceive Lua as exotic, JavaScript and Lua are the same technology. If we strip all the garbage from these languages, we get the same excellent core.
In JavaScript, we must avoid the prototype object and the keywords class and this. In Lua, one must stay away from metatable manipulations.
This way, JavaScript and Lua converge into a clean, minimalistic language, with support for object-oriented programming.
Here is DrakonTech's approach to working with classes.
DrakonTech creates a factory function that creates a new object called self. In this object, the code generator inserts the public methods. All private methods remain inside the factory function. Private methods cannot be called directly from outside.
The factory's parameters become the constructor parameters. The factory's local variables become the private fields of the class.
The object's methods can write directly to the properties of self object if the developer decides to do so. This way, the developer can keep state private or deliberately expose it through self.
Here is what generated class code looks like in JavaScript:
function Blue(time) { var self = { _type: 'Blue' }; var title; title = 'Dr.'; function buildGreeting(name) { return `Good ${ time }, ${ title } ${ name }!`; } function greeting(name) { var message; message = buildGreeting(name); console.log(message); } self.greeting = greeting; return self; } module.exports = { Blue };
And here is the Lua equivalent:
function Blue(time) local self = {_type="Blue"} local title title = "Dr." function buildGreeting(name) return "Good " .. time .. ", " .. title .. " " .. name .. "!" end function greeting(name) local message message = buildGreeting(name) print(message) end self.greeting = greeting return self end return { Blue = Blue }
And here is how to use this class in both languages:
blueObj = Blue("Morning") blueObj.greeting("Luna")
Note that in Lua, one does not use the colon notation to call a method, such as object:method(). The dot operator is used instead, just like in JavaScript: object.method().
To create a class in a JavaScript or Lua project in DrakonTech:
- Create a new folder.
- Create a function called class in that folder. The class function tells the code generator to build a class. The arguments of the class function become the constructor arguments. All variables declared in the class function become private fields of the class.
- Add functions to the folder. All functions inside this folder and its subfolders become the methods of the class.
- To make a method public, mark it as exported.
The name of the folder that contains the class function becomes the class name.
The class function (the class constructor) in JavaScript and Lua
The arguments and local variables of the class function are available to all methods of the class.
Public method
The self object is available both to the methods and to the class function.
If the class function is marked as exported, the class's factory function is exported. Then one can use the class outside of the module.
Note that in DrakonTech, you do not need to declare variables explicitly in Lua and JavaScript. They are declared automatically.