Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.82 KB | None | 0 0
  1. Window = {}
  2.  
  3. -- The prototype with default values
  4. Window.prototype = {x = 0, width = 100, height = 100}
  5.  
  6. -- Window Meta Table
  7. Window.mt = {}
  8.  
  9. -- Sets field/table 'o' with the metatable Window. The constructor function
  10. function Window.init(o) setmetatable(o, Window.mt)
  11.   return o
  12. end
  13.  
  14. --  Metatable Window.mt now has an __index field. This field takes a table and key and returns the appropriate key from the prototype
  15. Window.mt.__index = function (table, key) return Window.prototype[key] end
  16. -- Window.mt.__index = Window.prototype
  17.  
  18. -- table 'w' now gets the metatable Window.mt
  19. w = Window.init{x = 10, y = 20}
  20.  
  21. -- table 'w' cannot find a width field so it then checks its metatable Window.mt, Window.mt has the field __index that will take the requested missing key and return a value from the prototype
  22. print(w.width)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement