【问题标题】:How can I override a parent method in coffeescript while still being able to call the parent如何在咖啡脚本中覆盖父方法,同时仍然能够调用父方法
【发布时间】:2013-02-05 21:47:29
【问题描述】:

我有两个类似这样的咖啡脚本类。在基本视图模型中,我有一个方法要在从基本视图模型继承的子视图中覆盖。

class exports.BaseViewModel
    constructor: () ->

    someBaseMethod: =>
        console.log "I'm doing the base stuff"

class ChildViewModel extends BaseViewModel
    constructor: () ->

    someBaseMethod: =>
        @doSomethingFirst()
        super @someBaseMethod()

这不是按原样工作的,因为 super @someBaseMethod() 行调用自身创建一个无限循环。

这里可以实现我想要的吗?

【问题讨论】:

标签: coffeescript


【解决方案1】:

是的,调用super 就像它是一个函数一样(它表示对您所在方法的超类版本的引用):

class ChildViewModel extends BaseViewModel
  constructor: ->

  someBaseMethod: =>
    @doSomethingFirst()
    super()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-16
    • 2014-10-26
    • 2013-08-24
    • 1970-01-01
    • 2014-05-11
    • 1970-01-01
    相关资源
    最近更新 更多