Javascript 如何知道类何时通过Coffeescript extends继承?

Javascript 如何知道类何时通过Coffeescript extends继承?,javascript,inheritance,backbone.js,module,coffeescript,Javascript,Inheritance,Backbone.js,Module,Coffeescript,我希望采用与Ruby提供的静态方法相同的方法,正如您在其用于模块操作的文档中看到的那样: class Foo @inherited: (subclass) -> console.log('hey hou') class Hey extends Foo class Hou extends Foo 产出: => hey hou => hey hou 我如何用Coffeescript“扩展”实现这一点?我的意思是,如果我使用Backbone.js的“extend”

我希望采用与Ruby提供的静态方法相同的方法,正如您在其用于模块操作的文档中看到的那样:

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编译了它,这是不可能做到的

有什么想法吗?

没有

它以前有这个,它被移除了。有些人想把它放回去,但关于它需要如何工作却有很多有趣的地方

有关这方面的一些参考资料来源:

建议的解决方法依赖于子类的显式调用

class A extends B

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

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

亚历克斯·韦恩的回答是完全正确的

但是,如果您确实需要它(例如,出于调试目的),而不必进行显式函数调用,您还可以在每个文件的开头重新定义CoffeeScript编译器生成的
\u extends
函数。由于
\u 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);
`