Skip to content

Instantly share code, notes, and snippets.

@mattmccray
Created September 11, 2012 01:48
Show Gist options
  • Select an option

  • Save mattmccray/3695339 to your computer and use it in GitHub Desktop.

Select an option

Save mattmccray/3695339 to your computer and use it in GitHub Desktop.
My ES6 class definition proposal
// How about another ES6 class definition proposal? :-)
// What if the the `class` block is a closure?
class Monster extends Sprite {
// This is a block/closure so this creates a local/private variable:
let hitpoints= null;
constructor(name, health=0){
this.name = name;
hitpoints= health;
}
attack(target) {
// String substitution:
log(`The monster attacks ${target}`);
}
get isAlive() {
return hitpoints > 0;
}
set health(value) {
if(value < 0) {
throw new Error("Health must be non-negative");
}
hitpoints= value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment