Javascript “如何使用武力”;这";用咖啡脚本?
我需要在MotherClass中定义一个静态方法,如下所示:Javascript “如何使用武力”;这";用咖啡脚本?,javascript,coffeescript,this,static-methods,Javascript,Coffeescript,This,Static Methods,我需要在MotherClass中定义一个静态方法,如下所示: class @MotherClass @test = => Foo.bar(this) # same with @ function() { return Foo.bar(MotherClass); }; 但是如果你尝试一下,你会发现,这是在“MotherClass”中自动编译的 所以,这是一样的,但不是真的 事实上,我在继承MotherClass时有一个ChildClass @ChildClass
class @MotherClass
@test = =>
Foo.bar(this) # same with @
function() {
return Foo.bar(MotherClass);
};
但是如果你尝试一下,你会发现,这是在“MotherClass”中自动编译的
所以,这是一样的,但不是真的
事实上,我在继承MotherClass时有一个ChildClass
@ChildClass extends @MotherClass
因此定义了ChildClass.test()。但就像这样:
class @MotherClass
@test = =>
Foo.bar(this) # same with @
function() {
return Foo.bar(MotherClass);
};
我需要Foo.bar的第一个参数是ChildClass中的ChildClass(如果我设置ChildClass2 class…,则为ChildClass2),而不是MotherClass。
所以我需要的是动态的,而不是静态的
如何在CoffeeScript中书写“this”
thx
编辑:我找到了“burk!”解决方案^^=>“eval('this')”,但这真的很糟糕。如何做得更好?使用瘦箭头而不是胖箭头:
class @MotherClass
@test = ->
Foo.bar(this)
胖箭头使您的函数绑定到
MotherClass
哦。。。很容易。我认为=>是在这个上下文中保持类的必要条件,但不需要^^好的答案!您如何调用test
函数,以及您期望它中的是什么?test()是MotherClass(以及Childclass)的静态方法。查看代码^^^我希望“this”包含动态调用的类