Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Window = {}
- -- The prototype with default values
- Window.prototype = {x = 0, width = 100, height = 100}
- -- Window Meta Table
- Window.mt = {}
- -- Sets field/table 'o' with the metatable Window. The constructor function
- function Window.init(o) setmetatable(o, Window.mt)
- return o
- end
- -- Metatable Window.mt now has an __index field. This field takes a table and key and returns the appropriate key from the prototype
- Window.mt.__index = function (table, key) return Window.prototype[key] end
- -- Window.mt.__index = Window.prototype
- -- table 'w' now gets the metatable Window.mt
- w = Window.init{x = 10, y = 20}
- -- 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
- print(w.width)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement