如何称呼“超级”来自 CoffeeScript 中的回调

发布于 2024-11-17 12:05:28 字数 330 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

漫漫岁月 2024-11-24 12:05:28

恐怕,您提出的写出 Foo.__super__.a.apply(this, argument) 的解决方案基本上与您将得到的一样好。 CoffeeScript 允许您编写类似的内容

a = ->
  b = ->
    super

(在这种情况下 super 指向 b 的 super 函数),因此使用 super 会有点混乱code> 内

a: ->
  x =>
    super

指向 a 的超级函数。不过,您可以提出问题来要求允许这样做。从编译的角度来看,它并不含糊,只是看起来有点奇怪。

您可以尝试以下一些更优雅的方法:

class Foo
  constructor: ->
    @sup = Foo.__super__

  a: ->
    x =>
      @sup.a.apply this, arguments

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

a = ->
  b = ->
    super

(in which case super points to b's super function), so it would be a bit confusing to have super within

a: ->
  x =>
    super

point 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:

class Foo
  constructor: ->
    @sup = Foo.__super__

  a: ->
    x =>
      @sup.a.apply this, arguments
请恋爱 2024-11-24 12:05:28

我遇到了同样的问题并像这样解决了它:

ChainSuper = (classType, memberName, useFatArrow) ->
  original = classType::[memberName]
  superf  = classType.__super__.constructor.prototype[memberName]
  callSuper = (thisRef, args, superArgs) ->
    for i in [0...superArgs.length]
      args[i] = superArgs[i]
    superf.apply thisRef, args

  classType::[memberName] = if useFatArrow
    (args...) ->
      original.call @, args..., (superArgs...) =>
        callSuper @, args, superArgs
  else
    (args...) ->
      original.call @, args..., (thisRef, superArgs...) ->
        callSuper thisRef, args, superArgs

这是非常不符合犹太教规的,如果 Coffeescript 编译器的类语义发生变化,可能会破坏。然而,在等待的过程中,我可以说:

CheckSuper = (ref, message) ->
  if ref instanceof Superclass
    alert "Works with #{message}"

class Superclass
  plus: (values...) ->
    CheckSuper @, 'plus'
    @val = 0
    for i in values
      @val += i
    alert @val

  minusBase: 0
  minus: (values...) ->
    CheckSuper @, 'minus'
    @val = @minusBase
    for i in values
      @val -= i
    alert @val

class Subclass extends Superclass
  plus: (values..., Super) ->
    setTimeout (=> Super @), 0
  ChainSuper @, 'plus'

  minus: (values..., Super) =>
    @minusBase = values[0]
    setTimeout (-> Super values[1...]..., 0), 0
  ChainSuper @, 'minus', true


subInstance = new Subclass()
subInstance.plus 1, 2, 3, 4
minus = subInstance.minus
minus 100, 10, 1

请注意,使用此解决方案,如果在使用超过 n 个参数调用该方法时将 n 个参数传递给 Super(),则只有前 n 个参数得到在对 super 方法的调用中被覆盖,其余部分将不变地传递。

I've encountered the same problem and solved it like this:

ChainSuper = (classType, memberName, useFatArrow) ->
  original = classType::[memberName]
  superf  = classType.__super__.constructor.prototype[memberName]
  callSuper = (thisRef, args, superArgs) ->
    for i in [0...superArgs.length]
      args[i] = superArgs[i]
    superf.apply thisRef, args

  classType::[memberName] = if useFatArrow
    (args...) ->
      original.call @, args..., (superArgs...) =>
        callSuper @, args, superArgs
  else
    (args...) ->
      original.call @, args..., (thisRef, superArgs...) ->
        callSuper thisRef, args, superArgs

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:

CheckSuper = (ref, message) ->
  if ref instanceof Superclass
    alert "Works with #{message}"

class Superclass
  plus: (values...) ->
    CheckSuper @, 'plus'
    @val = 0
    for i in values
      @val += i
    alert @val

  minusBase: 0
  minus: (values...) ->
    CheckSuper @, 'minus'
    @val = @minusBase
    for i in values
      @val -= i
    alert @val

class Subclass extends Superclass
  plus: (values..., Super) ->
    setTimeout (=> Super @), 0
  ChainSuper @, 'plus'

  minus: (values..., Super) =>
    @minusBase = values[0]
    setTimeout (-> Super values[1...]..., 0), 0
  ChainSuper @, 'minus', true


subInstance = new Subclass()
subInstance.plus 1, 2, 3, 4
minus = subInstance.minus
minus 100, 10, 1

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.

流心雨 2024-11-24 12:05:28

现在你可以使用
超级::方法()
反而。

Now you can use
super::method()
instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文