Don't Invoke Inherited Method Twice In Es6 Classes
I'm moving from RequireJS to browserify (together with babelify) and try to rewrite my current modules to classes. For each of my RequireJS modules I have a method called eventHand
Solution 1:
Since you know the parent constructor calls this.eventHandler
, don't do so in the derived classes:
'use strict';
importTooltipfrom'./Tooltip';
classBlockextendsTooltip {
constructor() {
super();
// (No call here)
}
eventHandler() {
// Module specific events// Gets called twice
}
}
module.exports = Block;
Re your comment:
The parent classes don't always implement an eventHandler method. So I need to ensure it gets called in this case as well.
Where they do, don't do the call. Where they don't, do do the call. Subclasses are very tightly bound to superclasses, by their nature.
If you can assume (!) that the presence of eventHandler
in the super means it has called it from its constructor, you could do something like this:
constructor() {
super();
if (!super.eventHandler) {
this.eventHandler();
}
}
...but again, the nature of the super/sub relationship is that it's very tightly-bound, so relying on your knowledge of whether the super does this or not is reasonable.
Post a Comment for "Don't Invoke Inherited Method Twice In Es6 Classes"