Reputation: 580
I'm using a library that has a class defined like this:
RoundDown = function () {
this.prop = 5.5
this.do_something = function() {
return "base class: " + this.prop
};
};
Using CoffeeScript, I want to extend do_something so it will run its original function plus new code. Subclassing it seems like a clean solution, but the base class' function gets called, rather than the overridden function.
# Subclass overriding round function
class RoundUp extends RoundDown
do_something: () ->
return super() + " Subclass code"
What is the correct way to do? Thanks!
Upvotes: 2
Views: 442
Reputation: 4060
The problem is that RoundDown is setting a field of the instance, not of the prototype. The ideal way to fix this would be
RoundDown = function () {
this.prop = 5.5
};
RoundDown.prototype.do_something = function() {
return "base class: " + this.prop
};
But maybe you don't have permission to modify RoundDown, eg it is in a library, so do this
class RoundUp extends RoundDown
constructor: ->
super()
oldDoSomething = @do_something
@do_something = ->
oldDoSomething.call(this) + " Subclass code"
Upvotes: 2
Reputation: 15156
Using js2coffee converter: js2.coffee:
RoundDown = ->
@prop = 5.5
@do_something = ->
'base class: ' + @prop
Upvotes: 0