SORU
1 ŞUBAT 2012, ÇARŞAMBA


Ve coffeescript yöntemleri statik sınıflar

Coffeescript statik bir Yardımcı Sınıf yazmak istiyorum. Bu mümkün mü?

sınıf:

class Box2DUtility

  constructor: () ->

  drawWorld: (world, context) ->

kullanarak:

Box2DUtility.drawWorld(w,c);

CEVAP
1 ŞUBAT 2012, ÇARŞAMBA


@ ile başlatılması yeterli olurdu sınıf yöntemleri tanımlayabilirsiniz:

class Box2DUtility
  constructor: () ->
  @drawWorld: (world, context) -> alert 'World drawn!'

# And then draw your world...
Box2DUtility.drawWorld()

Demo: http://jsfiddle.net/ambiguous/5yPh7/

Ve eğer drawWorld bir kurucu gibi hareket etmek istiyorsanız, o zaman bu gibi new @ söyleyebilirsin:

class Box2DUtility
  constructor: (s) -> @s = s
  m: () -> alert "instance method called: #{@s}"
  @drawWorld: (s) -> new @ s

Box2DUtility.drawWorld('pancakes').m()

Demo: http://jsfiddle.net/ambiguous/bjPds/1/

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Ownage Pranks

    Ownage Prank

    13 AĞUSTOS 2007
  • talkandroid

    talkandroid

    27 Mayıs 2010
  • Thom Hall

    Thom Hall

    24 Kasım 2006