DEV Community

So Sun Park
So Sun Park

Posted on

1

addeventlistener with class method and bind

I wanted to write addeventlistener inside a class constructor. But then, javascript requires classMethod.bind(this) and that sometimes is annoying to be used with addeventListener because that also send 'event' to callback classMethod.

The biggest winner is

making classMethod as () => { } arrow function.
Then it wouldn't mess up your this. inside class method.
Enter fullscreen mode Exit fullscreen mode

https://stackoverflow.com/questions/21298918/is-it-possible-to-call-a-class-method-with-addeventlistener

Top comments (0)

👋 Kindness is contagious

Dive into this informative piece, backed by our vibrant DEV Community

Whether you’re a novice or a pro, your perspective enriches our collective insight.

A simple “thank you” can lift someone’s spirits—share your gratitude in the comments!

On DEV, the power of shared knowledge paves a smoother path and tightens our community ties. Found value here? A quick thanks to the author makes a big impact.

Okay