Static classes and methods in coffeescript

I want to write a static helper class in coffeescript. Is it possible?

Grade:

class Box2DUtility constructor: () -> drawWorld: (world, context) -> 

via:

 Box2DUtility.drawWorld(w,c); 
+83
coffeescript
Feb 01 2018-12-12T00:
source share
1 answer

You can define class methods by prefixing them with @ :

 class Box2DUtility constructor: () -> @drawWorld: (world, context) -> alert 'World drawn!' # And then draw your world... Box2DUtility.drawWorld() 

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

And if you want your drawWorld work as a constructor, you can say new @ as follows:

 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/

+173
Feb 01 '12 at 4:17
source share



All Articles