Harmony:classes static and private

Kam Kasravi kamkasravi at yahoo.com
Tue Jun 7 15:15:07 PDT 2011


In the harmony:classes proposal the following example is provided:

class Monster {
  // "static" places the property on the constructor.
  static allMonsters = [];
 
  // "public" declares on the prototype.
  public numAttacks = 0;
 
  // "private" places it on the private record of the new instance.
  private health;
}

According to the grammar it doesn't appear that static and private are allowed together. That is 
  static private allMonsters = [];
would be an error. However I think this is a common use case, for example Monster may wish to 
implement a constructor based dependency injection pattern as:

class Monster {
  constructor(name, health) {
    public name = name;
    private health = health;
    Monster.allMonsters.push(this);
  }
  static allMonsters = [];
}

class YourMammaMonster extends Monster {
  constructor(name,posse) {
    super(name, 10);
    private posse = posse;
  }
  set health(value) {
    private(this).health = value*0.5;
  }
}

In this scenario, Monster would want strong encapsulation of allMonsters. Have the authors considered this use case? Are their alternative patterns to achieve a similar result? 


More information about the es-discuss mailing list