【问题标题】: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 的讨论。 stackoverflow.com/questions/13243679
  • 与 Backbone.Marionette.Application::testMethod = -> console.log "I was here?" 相比有什么优势

标签: coffeescript prototype underscore.js marionette


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 2011-12-04
    • 1970-01-01
    • 2016-06-27
    • 2011-11-14
    • 2013-10-30
    • 2012-02-22
    • 2014-04-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多