CoffeeScript クラス定義メモ

class Hoge
    # private static member (Is accessible from all members.)
    _aaa = 0
    _bbb = 0
    # public static member
    @getAAA = () -> _aaa
    @getBBB: () -> _bbb
    # constructor
    constructor: (@a, @b, aaa) -> # Variables 'a' and 'b' are defined as public instance members.
        _aaa = aaa
        _bbb = aaa * 2
        # private instance member (Is not accessible from public methods.)
        _c = a * b
        # privileged instance member
        this.getC = () -> _c
    # public instance member
    getA: () -> @a
    getB: () -> @b

h1 = new Hoge(1, 2, 10)
h2 = new Hoge(3, 4, 20)
alert(h1.getA() + ", " + h1.getB() + ", " + h1.getC()) # 1, 2, 2
alert(h2.getA() + ", " + h2.getB() + ", " + h2.getC()) # 3, 4, 12
alert(Hoge.getAAA()) # 20
alert(Hoge.getBBB()) # 40

わかりづらい・・・。
全部 public にして、外部からアクセスして欲しくないメンバーには接頭辞に _ を付ける、ってスタンスがいいと思う。
あと、紛らわしいから static メンバーには @ じゃなくてクラス名を使う方がいいかな。

class Hoge
    # static member
    Hoge._aaa = 0
    Hoge._bbb = 0
    Hoge.getAAA = () -> Hoge._aaa
    Hoge.getBBB = () -> Hoge._bbb
    # constructor
    constructor: (@_a, @_b, aaa) ->
        Hoge._aaa = aaa
        Hoge._bbb = aaa * 2
        @_c = @_a * @_b
    # instance member
    getA: () -> @_a
    getB: () -> @_b
    getC: () -> @_c

h1 = new Hoge(1, 2, 10)
h2 = new Hoge(3, 4, 20)
alert(h1.getA() + ", " + h1.getB() + ", " + h1.getC()) # 1, 2, 2
alert(h2.getA() + ", " + h2.getB() + ", " + h2.getC()) # 3, 4, 12
alert(Hoge.getAAA()) # 20
alert(Hoge.getBBB()) # 40


[おまけ]
一つ目のスクリプトから変換された JavaScript

Hoge = (function() {
  var _aaa, _bbb;
  Hoge.name = 'Hoge';
  _aaa = 0;
  _bbb = 0;
  Hoge.getAAA = function() {
    return _aaa;
  };
  Hoge.getBBB = function() {
    return _bbb;
  };
  function Hoge(a, b, aaa) {
    var _c;
    this.a = a;
    this.b = b;
    _aaa = aaa;
    _bbb = aaa * 2;
    _c = a * b;
    this.getC = function() {
      return _c;
    };
  }
  Hoge.prototype.getA = function() {
    return this.a;
  };
  Hoge.prototype.getB = function() {
    return this.b;
  };
  return Hoge;
})();

h1 = new Hoge(1, 2, 10);
h2 = new Hoge(3, 4, 20);
alert(h1.getA() + ", " + h1.getB() + ", " + h1.getC());
alert(h2.getA() + ", " + h2.getB() + ", " + h2.getC());
alert(Hoge.getAAA());
alert(Hoge.getBBB());

二つ目のスクリプトから変換された JavaScript

Hoge = (function() {
  Hoge.name = 'Hoge';
  Hoge._aaa = 0;
  Hoge._bbb = 0;
  Hoge.getAAA = function() {
    return Hoge._aaa;
  };
  Hoge.getBBB = function() {
    return Hoge._bbb;
  };
  function Hoge(_a, _b, aaa) {
    this._a = _a;
    this._b = _b;
    Hoge._aaa = aaa;
    Hoge._bbb = aaa * 2;
    this._c = this._a * this._b;
  }
  Hoge.prototype.getA = function() {
    return this._a;
  };
  Hoge.prototype.getB = function() {
    return this._b;
  };
  Hoge.prototype.getC = function() {
    return this._c;
  };
  return Hoge;
})();

h1 = new Hoge(1, 2, 10);
h2 = new Hoge(3, 4, 20);
alert(h1.getA() + ", " + h1.getB() + ", " + h1.getC());
alert(h2.getA() + ", " + h2.getB() + ", " + h2.getC());
alert(Hoge.getAAA());
alert(Hoge.getBBB());