Is there any major benefit of using one over the other?
I asked this in another thread lately and Phanx posted the prototype trick which is kind of neat.
local function variant
Lua Code:
local function ResetAllModels()
for i, model in pairs(self.models) do
ModelReset(model)
end
end
local function ModelReset(...)
print("ModelReset",...)
end
local function ModelOnMouseDown(...)
print("ModelOnMouseDown",...)
end
local function ModelOnMouseUp(...)
print("ModelOnMouseUp",...)
end
local function CreateModel(parent,id)
local m = CreateFrame("PlayerModel", nil, parent)
m.id = id
m.name = "model"..id
m:SetScript("OnMouseDown", ModelOnMouseDown)
m:SetScript("OnMouseUp", ModelOnMouseUp)
end
local frame = CreateFrame("Frame")
frame.models = {}
for i=1, 50 do
frame.models[i] = CreateModel(frame,i)
end
object:Function variant
Lua Code:
local frame = CreateFrame("Frame")
function frame:ResetAllModels
for i, model in pairs(self.models) do
model:Reset()
end
end
frame.models = {}
--prototype
frame.modelPrototype = {}
function modelPrototype:Reset(...)
print("Reset",...)
end
function modelPrototype:OnMouseDown(...)
print("OnMouseDown",...)
end
function modelPrototype:OnMouseUp(...)
print("OnMouseUp",...)
end
function frame:CreateModel(id)
local m = CreateFrame("PlayerModel", nil, self)
m.id = id
m.name = "model"..id
for k, v in pairs(self.modelPrototype) do
m[k] = v
end
m:SetScript("OnMouseDown", m.OnMouseDown)
m:SetScript("OnMouseUp", m.OnMouseUp)
end
for i=1, 50 do
frame.models[i] = frame:CreateModel(i)
end
What I'm asking for is the following. If I add a function to an object...does it produce multiple copies of that function or is it by reference?
Using a local function set has the benefit that the function is only defined once but most of the time you need to add self as an argument which you do not need on object functions.
Well...event handler functions will still use self as an argument.
What do you pick and why?