开发者

Static methods & inheritance in Coffeescript

开发者 https://www.devze.com 2023-03-20 17:24 出处:网络
I\'ve been reading up a bit about coffeescript\'s inheritance model and I have the feeling I\'m on the fringes of an ideological debate which I really don\'t understand. So, I would be perfectly happy

I've been reading up a bit about coffeescript's inheritance model and I have the feeling I'm on the fringes of an ideological debate which I really don't understand. So, I would be perfectly happy to find out that I'm just doing things in the wrong way.

Basically what I am doing is writing a set of widgets which, among other things, need to handle events on their DOM elements. I thought a good way to go about this would be to have a class method which would be called once, to delegate all the events which the widget might need. The base widget class might have some simple click handlers, while the subclass might add to that some mouseover handlers or extra click handlers.

However, it appears that I'm not supposed to try and do the equivalent of calling super() inside a static method. There is a workaround which exists, (this.__super__.constructor.METHODNAME() but I've seen a lot of suggestions that this isn't the best way to do what I'm trying to do. Has anyone got any insights on how I should stru开发者_如何学Pythoncture this code? Keep using the workaround, or put all the delegation into a totally different place? I can't really just stick it in the prototype, since I won't necessarily have an instance to call the method on (or can I essentially still call a method on the prototype from a static context, like putting SwatchableWidget.prototype.delegateEvents() into an onload function or something?

Here's a bit of code to illustrate what I'm talking about:

class Widget
    @testProp: "ThemeWidget"
    @delegateEvents: ->
        console.log "delegate some generic events"

class SwatchableWidget extends Widget
    @testProp2 = "SwatchWidget"
    @delegateEvents: ->
        console.log "delegate some specific swatchable widget events"
        this.__super__.constructor.delegateEvents()

Widget.delegateEvents()
SwatchableWidget.delegateEvents()

Thanks for any help.


I suggest replacing

this.__super__.constructor.delegateEvents()

with

Widget.delegateEvents()

trying to use super to call static methods is not required (and doesn't make much sense)


I don't understand why delegateEvents would be a class-level method, or why Widget.delegateEvents have to be called again from SwatchableWidget.delegateEvents. If it's just class initialization code, you should put it in the class body directly:

class Widget
    console.log "delegate some generic events"
    ...
    @testProp: "ThemeWidget"

class SwatchableWidget extends Widget
    console.log "delegate some specific swatchable widget events"
    ...
    @testProp2 = "SwatchWidget"

I take it you're waiting for a specific DOM state before running this initialization code? Maybe I could suggest another approach if you told me a little bit more about the preconditions for delegateEvents.


It sounds like you want a different type of inheritance model where each inherited function of a certain type ("parent calling") will walk the inheritance tree and call all its parents with the same name.

You could call any direct parent functions in each child manually as you've written. Then it will float up the inheritance chain anywhere you specify such a relationship.

I would bind the parents delegate call in the constructor to a current class function

delegateparents => 
  #call any parent class methods
0

精彩评论

暂无评论...
验证码 换一张
取 消