如何称呼“超级”来自 CoffeeScript 中的回调
class Foo
a: ->
x.call =>
super
不会编译,因为我无法从匿名类调用 super 。然而我的 目的是调用“a”的超类方法。这是失踪吗 咖啡脚本的能力?
请注意,我将代码更改为
class Foo
a: ->
x.call =>
return Foo.__super__.a.apply(this, arguments)
使其工作,但这不是咖啡脚本正确的!
class Foo
a: ->
x.call =>
super
will not compile as I can't call super from anonymous class. However my
intention is to call the superclass method for 'a'. Is this a missing
capability in coffeescript?
Note that I change the code to
class Foo
a: ->
x.call =>
return Foo.__super__.a.apply(this, arguments)
to make it work but that just ain't coffeescript right!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
恐怕,您提出的写出 Foo.__super__.a.apply(this, argument) 的解决方案基本上与您将得到的一样好。 CoffeeScript 允许您编写类似的内容
(在这种情况下
super
指向b
的 super 函数),因此使用super
会有点混乱code> 内指向
a
的超级函数。不过,您可以提出问题来要求允许这样做。从编译的角度来看,它并不含糊,只是看起来有点奇怪。您可以尝试以下一些更优雅的方法:
Your proposed solution of writing out
Foo.__super__.a.apply(this, arguments)
is, I'm afraid, basically as good as you're going to get. CoffeeScript allows you to write things like(in which case
super
points tob
's super function), so it would be a bit confusing to havesuper
withinpoint to
a
's super function. You could raise an issue to ask for this to be allowed, though. It's not ambiguous from a compilation standpoint, just a bit odd-looking.Here's something you could try to be a little more elegant:
我遇到了同样的问题并像这样解决了它:
这是非常不符合犹太教规的,如果 Coffeescript 编译器的类语义发生变化,可能会破坏。然而,在等待的过程中,我可以说:
请注意,使用此解决方案,如果在使用超过 n 个参数调用该方法时将 n 个参数传递给 Super(),则只有前 n 个参数得到在对 super 方法的调用中被覆盖,其余部分将不变地传递。
I've encountered the same problem and solved it like this:
This is very non-kosher and will probably break if the Coffeescript compiler's class semantics change. While waiting for that, however, I'm able to say:
Please notice, that with this solution, if you pass n arguments to a Super() when the method has been called with more than n arguments, only the first n arguments get overwritten in the call to the super method and the rest will be passed on unchanged.
现在你可以使用
超级::方法()
反而。
Now you can use
super::method()
instead.