コンピュータクワガタ

かっぱのかっぱによるコンピュータ関連のサイトです

CoffeeScriptのclassでprivateなメソッド等を作る

自分のメモ用に、張っておく。
要するに、classの中に、オブジェクトのプロパティでなくてクロージャとして閉じ込めるということ。

class Test
  # private
  internal = 0

  # private method
  reset = () ->
    internal = 0

  inc: ->
    internal++
  view: ->
    alert "#{internal}"
  newView: ->
    reset()
    @view()

test = new Test()
test2 = new Test()
test.view()
test2.view()
test.inc()
test.view()
test2.view()
test.newView()