Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save houtianze/3f4c4c55b4a6f8f0a80f3fead71307c0 to your computer and use it in GitHub Desktop.
Save houtianze/3f4c4c55b4a6f8f0a80f3fead71307c0 to your computer and use it in GitHub Desktop.
// 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