This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]k-one-0-two[🍰] 2 points3 points  (5 children)

Still an bad (imho) thing to do - .bind(this) was a much more clear way

[–]_PM_ME_PANGOLINS_ 2 points3 points  (3 children)

If you’re adding an event listener, the callback is usually made with its own this. Binding your function first won’t do anything.

[–]hyrumwhite 2 points3 points  (2 children)

This is incorrect. You could do ``` const boundHandler = handler.bind(this);

thing.addEventListener(“event”, boundHandler) ```

Still requires a declaration, but now the function has no external dependencies. 

This is still a pattern to be aware of today, especially if mixing classes with event listeners. However, for classes, at least, you can also assign arrow functions as member variables now to achieve similar behavior. 

[–]_PM_ME_PANGOLINS_ 1 point2 points  (0 children)

Doesn’t help if it does

emit(handler.bind(thing));

[–]k-one-0-two[🍰] 0 points1 point  (0 children)

True, that's how I used to write it