Harmony classes

Kam Kasravi kamkasravi at yahoo.com
Sun Jun 5 22:59:05 PDT 2011


From the harmony classes example copied below for reference.
The set health(value) {...} assigns a new value to this.health. But health is a private property, so the assignment is setting a public property. Shouldn't the assignment be private(this).health = value?
class Monster {
  // The contextual keyword "constructor" followed by an argument
  // list and a body defines the body of the class’s constructor
  // function. public and private declarations in the constructor
  // declare and initialize per-instance properties. Assignments
  // such as this.foo = bar; set public properties.
  constructor(name, health) {
    public name = name;
    private health = 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 private(this).health > 0;
  }
 
  // Likewise, "set" can be used to define setters.
  set health(value) {
    if (value < 0) {
      throw new Error('Health must be non-negative.')
    }
    this.health = value
  }
 
  // An identifier optionally followed by "=" and an expression
  // declares a prototype property and initializes it to the value
  // of that expression. "public" as a member modifier is allowed.
  numAttacks = 0;
 
  // The keyword "const" followed by an identifier and an
  // initializer declares a constant prototype property.
  const attackMessage = 'The monster hits you!';
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20110605/23baf21f/attachment-0001.html>


More information about the es-discuss mailing list