Skip to content

Instantly share code, notes, and snippets.

@roine
Forked from anonymous/jsbin.utiqok.html
Created August 8, 2013 07:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save roine/6182464 to your computer and use it in GitHub Desktop.
Save roine/6182464 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>
</body>
</html>
/*
prototypal Inheritance
*/
var Villager, Swordman, Archer, Magician;
// Object.create for ie8
if (typeof Object.create !== 'function') {
Object.create = function (o) {
function F() {}
F.prototype = o;
return new F();
};
}
Villager = function(name){
this.name = name;
this.type = 1;
this.life = this.maxLife = 10;
this.strength = this.magic = this.dexterity = 1;
};
Villager.prototype.attack = function(victim) {
victim.life = victim.life - this.magic - this.dexterity - this.strength;
console.log(victim.name+ ' lost '+ (this.magic + this.dexterity + this.strength) + ' of life '+ ((victim.life <= 0) ? 0 : victim.life) + '/'+ victim.maxLife);
if(victim.life <= 0){
victim.life = 0;
console.log('Billy!...He died!');
}
};
Swordman = function(name) {
this.name = name;
this.type = 2;
this.life = this.maxLife = 100;
this.strength = 10;
this.magic = 1;
this.dexterity = 5;
}
Swordman.prototype = Object.create(Villager.prototype);
var jon = new Villager('jon');
var bob = new Villager('bob');
var bobFather = new Swordman('boby');
jon.attack(bob);
jon.attack(bob);
jon.attack(bob);
jon.attack(bob);
jon.attack(bobFather);
jon.attack(bobFather);
jon.attack(bobFather);
jon.attack(bobFather);
bobFather.attack(jon);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment