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:

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

class Ballgowninator extends Inator {
    constructor(whichCanvas) {
        super(whichCanvas, (e) => 
        console.log("ballgowninator mousedown"), (e) => 
        console.log("ballgowninator mouseup"));
    }    
}

class Yodelinator extends Inator {
    constructor(whichCanvas) {
        super(whichCanvas, (e) => 
        console.log("yodelinator mousedown"), (e) => 
        console.log("yodelinator mouseup"));
       
    }
   
}

const canvas = document.getElementById('canvas');
const b = new Ballgowninator(canvas);
const y = new Yodelinator(canvas);
<h1 id="canvas">CLICK ME!!!</h1>

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