Last active
August 29, 2015 14:24
-
-
Save neilcampbell/7d5ca4b619aa56e6b1bc to your computer and use it in GitHub Desktop.
ES6 Monster
This file contains 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
(function(target) { | |
let _health = Symbol(); | |
let _speed = Symbol(); | |
class Monster { | |
constructor(name, health, speed) { | |
this.name = name; | |
this[_health] = health; | |
this[_speed] = speed; | |
} | |
get speed() { | |
return this[_speed]; | |
} | |
get health() { | |
return this[_health]; | |
} | |
attack(monsterToAttack) { | |
this.takeDamage(1); | |
monsterToAttack.takeDamage(this.speed); | |
} | |
takeDamage(damageToTake) { | |
this[_health] = this[_health] - damageToTake; | |
if(this[_health] > 0) { | |
console.log(`${this.name} is still alive`); | |
} | |
else { | |
console.log(`${this.name} has died`); | |
} | |
} | |
} | |
target.Monster = Monster; | |
})(window); | |
var quickJames = new Monster('James', 10, 10); | |
var bigFred = new Monster('Fred', 100, 1); | |
quickJames.attack(bigFred); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment