scope == privacy (or not), so module scope works, as does a closure environment:
function Person(name) return { getName = function(self) return name end } end local p = Person("Dave") print(p:getName()) --\> "Dave" print(p.name) --\> "nil"
scope == privacy (or not), so module scope works, as does a closure environment:
function Person(name) return { getName = function(self) return name end } end local p = Person("Dave") print(p:getName()) --\> "Dave" print(p.name) --\> "nil"
Following X tutorial I see a convention used and I adopt it (as I’m new to Lua).
Then I need/want to see some related tutorial/video and the convention used is different (maybe not better or worse, just different), and it throws me off because then it creates doubt (which way is “right”, “correct”, “more accepted”, “best practices”).
So I am looking at a few function definition for Dusk at almost random:
function map.setCameraBounds(bounds) function map.setCameraFocus(f, noSnapCamera)
The above seem to be a hybrid of my first examples… more questions. But I don’t think I’m doing anything particularly wrong. functions = ‘:’ and properties = ‘.’ for now.
Your replies have been amazing and insightful. Thank you all so much.
As a C# developer, I like to think of the difference between . and : notation as the same as static and instance. This is because . functions won’t receive the ‘self’ and are more like free-standing methods or functions to be called on their own. Instance methods, on the other hand, have a notion of ‘self’ (‘this’ in C#) and are aware of the object of which they are a part. The definition in Lua is somewhat muddier, but it has it’s uses.