Advertisement
Qugurun

Closure Lua

Mar 3rd, 2024
842
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.55 KB | Source Code | 0 0
  1. print ("Closure Lua!")
  2.  
  3. function CreateUser()
  4.    local _name = "Qugurun"
  5.    
  6.    return {
  7.         setName = function(n)
  8.             _name = n
  9.         end,
  10.         getName = function()
  11.             return _name
  12.         end      
  13.     }
  14. end
  15.  
  16. local user1 = CreateUser()
  17. local user2 = CreateUser()
  18.  
  19. user1.setName("Laimadoo")
  20. print(user1.getName()) --> Laimadoo
  21. print(user2.getName()) --> Qugurun
  22. print(user2._name) --> nil
  23.  
  24. for k,v in pairs(user1) do
  25.     print(k,v) --> getName  function: ...
  26.                --> setName  function: ...
  27. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement