balupton
balupton

Reputation: 48680

How do I assign the super method to a variable without calling it in 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.

Upvotes: 3

Views: 131

Answers (1)

Alex Wayne
Alex Wayne

Reputation: 187074

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.

Upvotes: 4

Related Questions