Created
September 11, 2012 01:48
-
-
Save mattmccray/3695339 to your computer and use it in GitHub Desktop.
My ES6 class definition proposal
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
| // 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