Skip to content

Instantly share code, notes, and snippets.

@dagingaa
Created February 17, 2015 19:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dagingaa/ab167fa3c2093d043625 to your computer and use it in GitHub Desktop.
Save dagingaa/ab167fa3c2093d043625 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
}
}
@houtianze
Copy link

This is helpful. Unfortunately, the source link seems (strangely) dead now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment