-
-
Save houtianze/3f4c4c55b4a6f8f0a80f3fead71307c0 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
// Source: http://wiki.ecmascript.org/doku.php?id=strawman:maximally_minimal_classes | |
class Monster { | |
// A method named "constructor" defines the class’s constructor function. | |
constructor(name, health) { | |
// public name object | |
this.name = name; | |
// private name object | |
this[pHealth] = health; | |
} | |
// An identifier followed by an argument list and body defines a | |
// method. A “method” here is simply a function property on some | |
// object. | |
attack(target) { | |
log('The monster attacks ' + target); | |
} | |
// The contextual keyword "get" followed by an identifier and | |
// a curly body defines a getter in the same way that "get" | |
// defines one in an object literal. | |
get isAlive() { | |
return this[pHealth] > 0; | |
} | |
// Likewise, "set" can be used to define setters. | |
set health(value) { | |
if (value < 0) { | |
throw new Error('Health must be non-negative.') | |
} | |
this[pHealth] = value | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment