Coffeescript:引用实例方法中的类方法,无需硬编码
以下面的代码为例:Coffeescript:引用实例方法中的类方法,无需硬编码,coffeescript,Coffeescript,以下面的代码为例: class MyClass @staticMethod: -> hello_world() instanceMethod: -> MyClass.staticMethod() 有没有一种方法可以从内部instanceMethod引用MyClass而不显式命名它?类似于self.staticMethod()的内容,您可以使用它来获取“类”,就像在JavaScript中一样: instanceMethod: -> @constructor.
class MyClass
@staticMethod: -> hello_world()
instanceMethod: -> MyClass.staticMethod()
有没有一种方法可以从内部instanceMethod
引用MyClass而不显式命名它?类似于self.staticMethod()
的内容,您可以使用它来获取“类”,就像在JavaScript中一样:
instanceMethod: ->
@constructor.staticMethod()
演示(添加了子类以便更好地度量):