While LUA is not an object oriented programming language, you can mimick your code to act like one. The key here is with the setmetatable() function.

Here is a .lua file containing a simple class.

That’s it! That is all there is to it. Save it as object.lua and now you can simply create a new instance of your class like this:

When it came to a point where I had to use complex Javascript to handle objects, I had to use typeof to determine what kind of object it is.

I got confused at first why this certain variable that I used kept returning object. Further reading revealed that Javascript returns object for things which user may not consider to be objects like arrays.

Nulls also get a return value of object.

However, suppose your code already runs deep and you do not want to dig down your source codes just to check what type of object a variable is, you can use this line of code to determine the return type of the object.

That should do the trick.

Related Posts Plugin for WordPress, Blogger...