簡體   English   中英

帶有Coffeescript的基本繼承,未調用子重寫

[英]basic inheritance with coffeescript, child override not called

我收到了意外的行為。 我在子類中覆蓋某個函數,但仍在調用父類中的一個函數。 我究竟做錯了什么?

class MyClassA
    myFnc: ->
        debugger
        @myFncTest()

    myFncTest: ->
        ## this one is called eventhough it's defined in extended class

class MyClassB extends MyClassA
    myFncTest: ->
        debugger


inst = new MyClassB()
inst.myFnc()

編輯

我正在使用Marionette模塊,該模塊包裝兩個單獨的文件MyClassA和MyClassB

MyApp.module("MyModuleA", function(MyModule, MyApp, Backbone, Marionette, $, _)

class MyClassA
        myFnc: ->
            debugger
            @myFncTest()

        myFncTest: ->
            ## this one is called eventhough it's defined in extended class

MyApp.module("MyModuleB", function(MyModule, MyApp, Backbone, Marionette, $, _)

class MyClassB extends MyApp.MyModuleA.MyClassA

        myFncTest: ->
            debugger


    inst = new MyClassB()
    inst.myFnc()

如果我寫以下內容:

class MyClassA
    myFnc: ->
        console.log 'myFnc'
        @myFncTest()

    myFncTest: ->
        console.log 'hello from A'
        ## this one is called eventhough it's defined in extended class

class MyClassB extends MyClassA
    myFncTest: ->
        console.log 'hello from B'


inst = new MyClassB()
inst.myFnc()

並運行

➜ coffee test.coffee
myFnc
hello from B

我只收到“來自B的問候”。

我猜你是在調用super() ,還是它們在代碼中的其他副作用

我使用最新版本的coffeescript:

➜ coffee -v
CoffeeScript version 1.9.0

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM