Need your own opinion of using (or not using) this way of metatables

Hi!
I have been scouring through the forums and I’ve been looking at some cool modules. These people tend to use Way 1 (which I’m close to understanding), but I’ve been using Way 2. I want your own opinion if it’s better to script with Way 1 or Way 2. Are there disadvantages, advantages, tips and/or tricks that I need to know when I’m using way 1?

-- Way 1
...
function module.new()
	local self = setmetatable({}, {__index = module})

	self.variable1 = "blablabla"

	return self
end

function module:blablabla()
	print(self.variable1)
end
...
-- Way 2
...
function module.new()
	local new = {}

	new.variable1 = "blablabla"
	
	function new:blablabla()
		print(self.variable1)
	end

	return new
end
...
Which way is better to script with?
  • Way 1
  • Way 2

0 voters

It’s mostly for optimization.

With way 2, you have to create new methods for every instance of the class, while you don’t do that with way 1.

1 Like