Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
// Source:
class Monster {
// A method named "constructor" defines the class’s constructor function.
constructor(name, health) {
// public name object = 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

This comment has been minimized.

Copy link

commented Jul 21, 2017

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
You can’t perform that action at this time.