2013-04-06 4 views
23

をハードコーディングせずにインスタンスメソッドから参照クラスのメソッド:のCoffeeScript:例として、次のコードを撮影

class MyClass 

    @staticMethod: -> hello_world() 

    instanceMethod: -> MyClass.staticMethod() 

は、私は明示的に名前を付けずにinstanceMethod内部からのMyClassを参照することができ方法はありますか? self.staticMethod()

答えて

35

のようなものあなただけのJavaScriptのように "クラス" で取得するにはconstructorを使用することができます。

instanceMethod: -> 
    @constructor.staticMethod() 

デモ(良い測定のために追加サブクラスで):http://jsfiddle.net/ambiguous/zM3ND/

関連する問題