Skip to content

Instantly share code, notes, and snippets.

@neilcampbell
Last active August 29, 2015 14:24
Show Gist options
  • Save neilcampbell/7d5ca4b619aa56e6b1bc to your computer and use it in GitHub Desktop.
Save neilcampbell/7d5ca4b619aa56e6b1bc to your computer and use it in GitHub Desktop.
ES6 Monster
(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