Created
August 8, 2013 07:46
-
-
Save anonymous/6182459 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset=utf-8 /> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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