Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static methods & inheritance in Coffeescript

Tags:

coffeescript

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 structure 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.

like image 356
Jeremy Warne Avatar asked Jul 14 '11 06:07

Jeremy Warne


People also ask

Why we use static methods?

A static method has two main purposes: For utility or helper methods that don't require any object state. Since there is no need to access instance variables, having static methods eliminates the need for the caller to instantiate the object just to call the method.

What are static methods Java?

A static method in Java is a method that is part of a class rather than an instance of that class. Every instance of a class has access to the method. Static methods have access to class variables (static variables) without using the class's object (instance). Only static data may be accessed by a static method.

What are static and non static methods?

A static method is a class method and belongs to the class itself. This means you do not need an instance in order to use a static method. A non-static method is an instance method and belongs to each object that is generated from the class.

What is class and static method?

Class method can access and modify the class state. Static Method cannot access or modify the class state. The class method takes the class as parameter to know about the state of that class. Static methods do not know about class state. These methods are used to do some utility tasks by taking some parameters.


3 Answers

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)

like image 187
liammclennan Avatar answered Nov 05 '22 00:11

liammclennan


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.

like image 28
Trevor Burnham Avatar answered Nov 05 '22 00:11

Trevor Burnham


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
like image 1
Mark Essel Avatar answered Nov 04 '22 22:11

Mark Essel