I try to put some html element object in a class und
write addEventListener which should work for all instances.
I found a solution, but I don´t understand it.
eg.
class ValueSlider extends HTMLDivElement {
constructor() {
. . .
this.startDrag = function(el, evt) {
let thisNow = this; // class object
let element = el; // mySvg object
let event = evt; // event object
. . .
}
this.evHandler = this.startDrag.bind(this, this.mySvg);
this.mySvg.addEventListener("mousedown", this.evHandler );
}
}
Why the function receives the event object as last parameter ?
Thank you for discussion
Erhie
Top comments (3)
Is it possible to see the rest of the code for the this.startDrag? When you use addEventListener you can pass in three parameters:
As I read your code currently you could also implement it like this-
this.mySvg.addEventListener('mousedown', this.startDrag.bind(this));
Without seeing what startdrag is doing completely and what behaviour you want to achieve on the event, it is a bit hard to clarify any further.
a more complete example:
Thanks - When you are working with a function from an event handler it is not uncommon to see a parameter called event. Sometimes this is called e or in your case evt. It represents the event object. It is passed automatically and provides you with extra details/functionality. It is not to do with bind but more from the event handler itself. You can find more details here developer.mozilla.org/en-US/docs/L...
Does this help?