Closed ceremcem closed 5 years ago
The super
keyword refers to the current instance and method; see the documentation. So you can write:
class Y extends X
(@id) ->
load: (data) ->
super ... # basically shorthand for X::load ...
alert "data (#{data}) is loaded in Y (id: #{@id})"
Wow! Yes, that was what I meant. I've read exactly that part in the documentation just right before opening the issue but it turns out I didn't understand what I read. Thanks!
Is there a way to point current class' methods? Considering the following example:
class Z
load: (data) ->
alert "Z loads data: #{data}"
class X extends Z
->
super ...
SOMEHOW.load 'hello' # will alert "data (hello) is loaded in X (id: 5)"
load: (data) ->
alert "data (#{data}) is loaded in X (id: #{@id})"
class Y extends X
(@id) ->
super ...
load: (data) ->
# should call X.load first
#super ...
unless data is \something
alert "NOT EXPECTED: data (#{data}) is loaded in Y (id: #{@id})"
class T extends Y
(@id) ->
super ...
load: (data) ->
alert "data loaded in T: #{data}"
hello = new T 5
..load \something
I need to display data (hello) is loaded in X (id: 5)
by calling SOMEHOW.load 'hello'
.
To do that exact thing, I would probably write ::load.call this, 'hello'
, although this strikes me as an unusual pattern for object-oriented code, and if possible I think I would instead pull code in X::load
out into a different method which is called by X::load
and the X
constructor, and call that method the normal way.
Sometimes we need chain reaction instead of simply overwriting existing methods in class inheritance. I'd expect to have a
super.method()
ability but it seems it doesn't exist at the moment. Consider the following example:Here we want every class that
extends X
(orextends Class
whichextends X
under the hood) have a.load()
method that will both run its superclass'.load()
method and then perform its custom actions. Here we needsuper.load()
method.Workaround
Either use a
_super()
function that brings the superclass method with appropriate context:...or make your class implement
_super
method: