isovector / adventure

an open-source MOAI based point-n-click graphical adventure engine
Other
26 stars 3 forks source link

Properly prototype objects #8

Closed isovector closed 12 years ago

isovector commented 12 years ago

Currently in lua we have methods like

function xxx.prototype(x)
    function x.foo()
        ....
    end
end

-- usage
x = xxx.prototype({ })
x.foo()

This is wasteful, as it means we have a copy of every function for every instance. Instead we should prototype objects more like the following:

Xxx = { }
Xxx.__index = Xxx

function Xxx.create()
    local x = { }
    setmetatable(x, Xxx)
    return x
end

function Xxx:foo()
    ...
end

-- usage:
x = Xxx.create()
x:foo()
isovector commented 12 years ago

Done as of 62dbff78314a38362669a45ce921e244872a593a