2013-02-18 25 views
2

我想有一個Ruby提供了一個靜態方法'inherited'你自己的模塊的操作目的文檔中看到相同的方法:如何知道何時Coffeescript繼承了Class?

class Foo 
    @inherited: (subclass) -> 
    console.log('hey hou') 

class Hey extends Foo 

class Hou extends Foo 

輸出:

=> hey hou 
=> hey hou 

我怎樣才能實現這個目標與Coffeescript'延伸'?我的意思是,如果我使用Backbone.js的'extend'方法,我可以覆蓋它..但Coffeescript編譯它,這是不可能的。

有什麼想法?

回答

2

亞歷克斯韋恩的答案是完全合法的,也是正確的做法。但是,如果您確實需要它(例如,爲了調試目的)而不必進行顯式函數調用,則還可以在每個文件的開始處重新定義由CoffeeScript編譯器生成的函數__extends。由於__extends是CoffeeScript中的保留關鍵字,因此必須使用普通JavaScript重新定義,並使用反引號將其嵌入到CoffeeScript文件中:

` 
__extends = (function (extend) { 
    return function (child, parent) { 
     // Do actual heritage 
     var result = extend(child, parent); 
     // Do something with child or parent 
     if (parent.inherited instanceof Function) parent.inherited(child); 
     // Return the result as in the original '__extends' function 
     return result; 
    } 
})(__extends); 
` 
1

沒有。

它曾經有這個,它被刪除。有些人希望回來,但有關它需要如何工作的幻想。

從源頭上一些有關此引用:

的建議的解決方法依賴於從兒童類的顯式調用。

class A extends B 

    # child class calls extended hook of parent class explicitly. 
    B.extended(this) 

    classBody: -> 
    methods: -> 
    goHere: ->