Skip to content Skip to sidebar Skip to footer

Can A Child Class Respond To Events Captured By Its Super?

I have a custom class which I am extending for various purposes, and the following code is working just fine: class Inator { constructor(whichCanvas) { this.myCanvas =

Solution 1:

In your parent class add the listener to your canvas, and in the constructor, you can pass the listener actions.

You can do something like this:

classInator {
    constructor(whichCanvas, mouseDown, mouseUp) {
        this.myCanvas = whichCanvas;
        this.myCanvas.addEventListener("mousedown", mouseDown);
        this.myCanvas.addEventListener("mouseup", mouseUp);
    }
}

classBallgowninatorextendsInator {
    constructor(whichCanvas) {
        super(whichCanvas, (e) =>console.log("ballgowninator mousedown"), (e) =>console.log("ballgowninator mouseup"));
    }    
}

classYodelinatorextendsInator {
    constructor(whichCanvas) {
        super(whichCanvas, (e) =>console.log("yodelinator mousedown"), (e) =>console.log("yodelinator mouseup"));
       
    }
   
}

const canvas = document.getElementById('canvas');
const b = newBallgowninator(canvas);
const y = newYodelinator(canvas);
<h1id="canvas">CLICK ME!!!</h1>

Post a Comment for "Can A Child Class Respond To Events Captured By Its Super?"