Event Handling With Subclasses
Based on this question. 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
Solution 1:
When I now extend this class and implement a
checkBreakpoint
with a different someBreakpoint in the subclass, thebindSpecificEvents
andunbindSpecificEvents
will still get called when thesuper
methods get invoked.
You may have a misunderstanding here. Even in superclass code, this.checkBreakpoint
will look up the checkBreakpoint
property on the object, find it on the object's immediate prototype (the subclass's), and call that version of checkBreakpoint
.
Here's a simpler example (live copy on Babel's REPL):
classBase {
constructor() {
this.method1();
}
method1() {
this.method2();
}
method2() {
console.log("Base#method2");
}
}
classDerivedextendsBase {
method2() {
console.log("Derived#method2");
}
}
newDerived;
Output:
Derived#method2
Note how the call in Base#method1
to this.method2
calls Derived#method2
, not Base#method2
. This is vital to polymorphism.
Post a Comment for "Event Handling With Subclasses"