2015-04-05 60 views
0

我收到意想不到的行为。在我的子类中覆盖了一个函数,但父类中的函数仍然被调用。我究竟做错了什么?基本继承与coffeescript,儿童覆盖不叫

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() 

编辑

我使用如果我写了下面这包装在两个单独的文件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() 

回答

1

木偶模块:

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 
+0

谢谢您的回答。我唯一的其他具体环境差异是我使用木偶模块看编辑 – user391986 2015-04-05 14:23:43

+0

啊它的工作!它只在我将咖啡标记升级到1.9.1时开始工作。我之前在1.8.0版本,并没有像上面描述的那样工作。谢谢 – user391986 2015-04-05 19:05:52