Originally Posted by semlar
Calling a function is one of the slowest things you can do in lua, and in your example you create a function which is only used to create a table and thrown away.
What do you gain by writing it this way?
|
Well, mostly a combination of OOP and structure/organization. Useful for "static" classes, as there's only one instance, as opposed to using some of the actual "class" examples, which also use an additional function to initialize.
Also like those so-called classes, you have the ability to control private or public methods/properties, as you only expose what you return.
The question I guess, would be - would it be any slower than something like this?
Code:
foo = {};
function foo:new()
local self = {};
self.a = "Something";
self.b = function() return "bob" end
return self;
end
local bar = foo:new();
Which is the common method of making "classes" in Lua, when unique instances are required.