简体   繁体   English

如何从同一对象的另一个mixin中调用在一个CoffeeScript mixin中定义的方法?

[英]How can I call a method defined in one CoffeeScript mixin from another mixin on the same object?

I have a CoffeeScript object thas has two mixins included: 我有一个CoffeeScript对象,其中包含两个mixins:

namespace 'S.Graphs', (exports) ->
  class exports.DocketEvents extends Module
    @extend Scatter
    @extend Axis

    name: 'DocketEvent'

    constructor: ({@litigation}) ->
      console.log("Building view of docket events for #{@litigation.name}")

How can I call a method defined in one mixin from another? 如何从一个混入中调用另一个定义的方法? I would expect that if I defined a method in one mixin, I could call it from the other: 我希望,如果我在一个mixin中定义了一个方法,则可以从另一个中调用它:

window.Scatter =
  extended: ->
    @include

      dimensions: ->
        Justly.view.dimensions()

But I can't as this throws an exception that this.dimensions is not a function. 但是我不能,因为这引发了一个例外,那就是this.dimensions不是一个函数。

How come? 怎么会?

Also, if I don't define my mixin with window. 另外,如果我没有用window.定义我的mixin window. as a prefix, I can't seem to use it as a mixin - what's the best way to resolve that? 作为前缀,我似乎无法将其用作mixin-解决该问题的最佳方法是什么?

Although this does not directly answer your question as asked, I believe it will give you a tool to fix your underlying problem. 尽管这不能按照要求直接回答您的问题,但我相信它将为您提供解决潜在问题的工具。 I had a situation crop up recently where I had to create several classes that each inherited from a google maps constructor. 最近我遇到了一种情况,我不得不创建几个类,每个类都从google maps构造函数继承。 But in order to DRY my own code I wanted all of those classes to also inherit their common functionality from an abstract base class. 但是为了干燥我自己的代码,我希望所有这些类也从抽象基类继承它们的通用功能。 Here's more or less what I came up with (simplified for example): 这或多或少是我想到的(例如,简化):

mixin = (obj, mixes...) ->
    mixes.forEach (mix) ->
        Object.keys(mix).forEach (key) -> obj[key] = mix[key]
    return obj

class MyBaseClass
    constructor: () ->
        #init some vars

    method1: () -> #do stuff
    method2: () -> #do stuff

class Signal extends google.maps.Marker
    constructor: () ->
        super()                            #adds google maps goodness
        MyBaseClass.apply(this, arguments) #inits the vars
        @type = 'signal'

    signalMethod1: () -> #do stuff

#add base class methods. Messing directly with class prototypes is considered by
#some to be something of an antipattern in coffeescript, but it achieves the 
#desired effect here.
mixin Signal.prototype, MyBaseClass.prototype

You may have to tweak the application order in the constructors of each of your derived classes (does the call to super come before or after applying the MyBaseClass constructor? what about the other constructor expressions?) but enables multiple inheritance. 您可能必须在每个派生类的构造函数中调整应用程序顺序(对super的调用是在应用MyBaseClass构造函数之前还是之后?其他构造函数表达式又如何?),但启用了多个继承。 Multiple inheritance usually is more trouble than its worth but sometimes is the best solution. 多重继承通常比其价值更大,但有时是最好的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM