Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Coffeescript: Reference class method from instance method without hardcoding

Tags:

coffeescript

Taking the following code as example:

class MyClass

  @staticMethod: -> hello_world()

  instanceMethod: -> MyClass.staticMethod()

Is there a way I could reference MyClass from inside instanceMethod without explicitly naming it? something like self.staticMethod()

like image 651
Marcel M. Avatar asked Apr 06 '13 03:04

Marcel M.


1 Answers

You can use constructor to get at the "class" just like in JavaScript:

instanceMethod: ->
    @constructor.staticMethod()

Demo (with added subclassing for good measure): http://jsfiddle.net/ambiguous/zM3ND/

like image 118
mu is too short Avatar answered Sep 17 '22 21:09

mu is too short