Coffeescript-在onclick中引用类方法
我只是想知道我怎样才能让它工作 尝试在onclick中引用方法Coffeescript-在onclick中引用类方法,coffeescript,Coffeescript,我只是想知道我怎样才能让它工作 尝试在onclick中引用方法 class C @f: () -> alert 'works' null constructor: () -> console.log @f # why is this undefined? document.onclick = @f new C() 这是因为@f编译成这个.f,而这就是构造函数本身 要访问类方法f,必须编写C.f: 我假设您想要绑定一个实例方法,而不是类方
class C
@f: () ->
alert 'works'
null
constructor: () ->
console.log @f # why is this undefined?
document.onclick = @f
new C()
这是因为@f编译成这个.f,而这就是构造函数本身
要访问类方法f,必须编写C.f:
我假设您想要绑定一个实例方法,而不是类方法
class C
#this defines a class method
@f: () ->
alert 'works'
null
#this is an instance method
f: () ->
alert 'works'
null
constructor: () ->
console.log @f # why is this undefined?
document.onclick = @f
new C()
如果不想拖拽显式类名,也可以使用@constructor.f。
class C
#this defines a class method
@f: () ->
alert 'works'
null
#this is an instance method
f: () ->
alert 'works'
null
constructor: () ->
console.log @f # why is this undefined?
document.onclick = @f
new C()