タグ

ブックマーク / d.hatena.ne.jp/ogatatsu (1)

  • 無名クラスとSingleton - チラシの裏です

    CoffeeScriptは以下のように書くと無名クラスを書けるっぽい #無名クラスを定義して変数Hogeに代入する Hoge = class func: () -> alert "Hello World!" (new Hoge).func() //結果 var Hoge; Hoge = (function() { function _Class() {} _Class.prototype.func = function() { return alert("Hello World!"); }; return _Class; })(); (new Hoge).func(); そこでこの無名クラスを直接newしたのを代入すれば簡単にSingletonを作れる Fuga = new class func: () -> alert "Hello, World!" Fuga.func() //結果 v

  • 1