Is there any difference between these two types of declarations performance-wise?
local object = newObject()
function object:method(params)
end
local object:method = function(params)
end
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Yes, there is a difference. The second one doesn’t compile. So it has zero performance 😉
A “method declaration” is just syntactical sugar in Lua. These are identical:
But that sugar only works if you are naming the function as part of the function declaration.
The ‘:’ syntax for accessing “methods” in Lua only works for accessing functions that are stored in a table, named by a string key. You cannot use this syntax to set the value of a table.
Or, to put it another way, there is no other way to do this:
without explicitly specifying a ‘self’ parameter as the first parameter.