静态类和方法在coffeescript

我想在coffeescript中写一个静态辅助类。这可能吗?

类:

class Box2DUtility

  constructor: () ->

  drawWorld: (world, context) ->

使用:

Box2DUtility.drawWorld(w,c);
您可以通过用@添加前缀来定义类方法:

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

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

演示:http://jsfiddle.net/ambiguous/5yPh7/

如果你想让你的drawWorld像一个构造函数,那么你可以这样说:

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

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

演示:http://jsfiddle.net/ambiguous/bjPds/1/

http://stackoverflow.com/questions/9090531/static-classes-and-methods-in-coffeescript

本站文章除注明转载外,均为本站原创或编译
转载请明显位置注明出处:静态类和方法在coffeescript