【问题标题】:Calling other super method directly when coming from sub来自 sub 时直接调用其他 super 方法
【发布时间】:2015-11-27 02:03:34
【问题描述】:

upperclass = class Superclass
  constructor: () ->

  @somemethod: () ->
    console.log "I'm in super"

  @someothermethod: () ->
    console.log "I'm trying to reach super without going through sub!"
    @somemethod()

lowerclass = class Subclass extends Superclass
  constructor: () ->

  @somemethod: () ->
    console.log "I'm in sub but only want to go to super! I.e. not through sub, calling super here!"

  @someothermethod: () ->
    super

lowerclass.someothermethod()

我希望@somemethod 调用直接转到超级方法,避免使用子方法。那可能吗?在某些方法声明中添加了@,以使其与 CoffeeScript 测试器 here 一起使用。

【问题讨论】:

    标签: javascript class oop inheritance coffeescript


    【解决方案1】:

    可以直接参考Superclass.somemethod

    Superclass.somemethod.call @
    

    我认为不命名Superclass是不可能(或必要的)。

    【讨论】:

    • 谢谢!最后只是在子类@somemethod 中添加和过滤可选参数。这些方法很大程度上依赖于实例,所以我想这可能是更合适的解决方案。无论如何,很高兴知道可以引用 SuperClass。
    • 不应该是Superclass::somemethod吗?
    • @Bergi:我认为它们是类方法 (@)。
    • @minitech:哦,当然。我不知道 OP 是否有意这样做,或者这是否是他尝试的副作用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-03
    • 1970-01-01
    • 2018-09-12
    • 2014-11-07
    • 2018-09-09
    • 1970-01-01
    • 2016-07-12
    相关资源
    最近更新 更多