3 ms·
as a note this works in JS too. var mariner= {}; mariner.new = function(){ var self = {}; var maxhp = 200; var hp = maxhp;
by Avshalom 14y ago
as a note this works in JS too.
var mariner= {};
mariner.new = function(){
var self = {};
var maxhp = 200;
var hp = maxhp;
self.heal = function(deltahp) {
if (hp + deltahp < maxhp){
hp = hp + deltahp;
} else {
hp = maxhp;
}
}
self.sethp = function(newhp) {
if (newhp < maxhp){
hp = newhp;
} else {
hp = maxhp;
}
}
self.gethp = function(){
return hp;
}
return self;
}
var m1 = mariner.new();
m1.sethp(100);
m1.heal(13);
alert(m1.gethp());
(shamelessly stolen from http://lua-users.org/wiki/ObjectOrientationClosureApproach http://lua-users.org/wiki/ObjectOrientationClosureApproach)