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
생성자처럼 행동 하고 싶다면 다음 new @
과 같이 말할 수 있습니다 .
class Box2DUtility
constructor: (s) -> @s = s
m: () -> alert "instance method called: #{@s}"
@drawWorld: (s) -> new @ s
Box2DUtility.drawWorld('pancakes').m()