Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I assign the super method to a variable without calling it in CoffeeScript?

Tags:

coffeescript

In CoffeeScript, I'd like to be able to assign the super method to a variable without calling it.

class a
  one: ->

class b extends a
  one: ->
    mySuper = super

However doing the following actually calls the super method rather than returning it - here's the compiled code:

return mySuper = b.__super__.one.apply(this, arguments);

How do I actually assign the super method to a variable rather than calling it?

I know I could do:

class b extends a
  one: ->
    mySuper = b.__super__.one

But it isn't that clean.

like image 759
balupton Avatar asked Dec 17 '25 17:12

balupton


1 Answers

Coffee script provides no syntax sugar for this use case. So do it yourself.

I would do it like this:

class B extends A
  one: ->
    mySuper = A::one
    mySuper.call this # calls the saved super method

Example

:: is a shorthand for prototype. So A::one compiles to A.prototype.one which is where your super method actually is.


But this seems like a red flag to me. I can't think of a case where this would be a good idea. I'd wager it's not part of the language because if you design your classes properly, you shouldn't need this. You say you want something clean, but the thing you want to do here I wouldn't consider clean at all.

like image 77
Alex Wayne Avatar answered Dec 19 '25 11:12

Alex Wayne



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!