有没有办法以更像咖啡脚本的方式扩展原型?

Posted

技术标签:

【中文标题】有没有办法以更像咖啡脚本的方式扩展原型?【英文标题】:Is there a way to extend prototypes in a more coffee script like manner? 【发布时间】:2014-01-20 10:16:41 【问题描述】:

通常,在使用 Coffee Script 时,我会像这样向 Backbone.Marionette.Application 添加方法...

do (Backbone) ->
  _.extend Backbone.Marionette.Application::,
    testMethod: ->
      console.log "I was here"

我想知道是否有更“Coffee Script'ish”的方式来做到这一点。带有extends关键字的IE,不使用下划线extend。

【问题讨论】:

你为什么不继承Backbone.Marionette.Application呢? Monkey patching 很简洁,但它不应该是您使用的第一个工具。 之前关于使用 Coffeescript extends 与 Backbone extend 的讨论。 ***.com/questions/13243679 与 Backbone.Marionette.Application::testMethod = -> console.log "I was here?" 相比有什么优势 【参考方案1】:

正如您所写,没有对象可以扩展您的应用程序,但如果您想创建一个“子类化”对象,您可以执行类似的操作

class MyApp extends Backbone.Marionette.Application
  testMethod: -> console.log "I was here"

这基本上是@mu 在他的评论中所建议的。

【讨论】:

以上是关于有没有办法以更像咖啡脚本的方式扩展原型?的主要内容,如果未能解决你的问题,请参考以下文章