What is private member mean

Summary

HELLO AND I WOULD LIKE TO ASK WHAT IS A ‘private member’ THIS SITE TELLS TO PUT ‘_camelCase’ FOR private members’

Adding an underscore before a variable doesn’t really do anything but by private member means an object/variable which is only accessed by module or script and not by the script calling it. Basically it is private and not used by public part of the API directly when it is called. It is a common practice used in luau and other languages.
For example;

local globalVariable1 = 0

local function foo()
     local _globalVariable1 = 5 -- Notice the underscore,  Now this variable is 'private'
     print(globalVariable1, _globalVariable1)
end

do foo() end

Now this isnt a really good example because by adding local we are creating a new variable anyway so no need for a private one.

So private variables are mostly used for getting a copy of a variable.

You can name some functions like this too.

local function _bar()
    print("private function")
end

do _bar() end



In conclusion it doesnt do anything, its just a style and used widely so this makes understanding of your code easier by other people thats why you should try to follow this.

3 Likes

Lua doesn’t have the theory of private members, but they can still be understood as private

local SomeClass
do
	SomeClass = setmetatable({}, {
		__tostring = function()
			return "SomeClass"
		end,
	})
	SomeClass.__index = SomeClass
	function SomeClass.new(...)
		local self = setmetatable({}, SomeClass)
		self:constructor(...)
		return self
	end
	function SomeClass:constructor()
		self._someprivatemember = true
	end
end

Basically saying:

class SomeClass {
private:
    bool _someprivatemember;
public:
    SomeClass() : _someprivatemember(true) {}
}
2 Likes