Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why do you bind ”this“ in event handler #26711

Closed
canfeit opened this issue Mar 17, 2019 · 5 comments
Closed

Why do you bind ”this“ in event handler #26711

canfeit opened this issue Mar 17, 2019 · 5 comments
Labels
events Issues and PRs related to the events subsystem / EventEmitter. question Issues that look for answers.

Comments

@canfeit
Copy link

canfeit commented Mar 17, 2019

Reflect.apply(handler, this, args);

Why do you bind ”this“ here?It is not compatible with browser.

@addaleax addaleax added the events Issues and PRs related to the events subsystem / EventEmitter. label Mar 17, 2019
@addaleax
Copy link
Member

Why do you bind ”this“ here?

Mostly because it’s often useful, I assume – it means you don’t have to store the object in another way, and often you can avoid having a closure this way.

It is not compatible with browser.

Node’s EventEmitter API doesn’t match the browser’s EventTarget API in a number of ways – that’s unfortunate, but it’s not something we could reasonably change easily.

I’d say there’s a chance that EventTarget could be introduced into Node as it adopts more and more Browser APIs, but too much code relies on how the current EventEmitter API works for us to make changes to that.

@himself65
Copy link
Member

it’s a best way when you extend EventEmitter

@devsnek
Copy link
Member

devsnek commented Mar 18, 2019

just to clarify, events emitted by EventTarget also bind this to the target, so there's not actually any difference here.

@Fishrock123
Copy link
Contributor

Seems like not an issue? Closing...

@Fishrock123 Fishrock123 added the question Issues that look for answers. label Mar 18, 2019
@jimmywarting
Copy link

I’d say there’s a chance that EventTarget could be introduced into Node

Would really like to see EventEmitter etc being implemented in node too so that ppl building cross browser/platform works the same way without any additional dependencies

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
events Issues and PRs related to the events subsystem / EventEmitter. question Issues that look for answers.
Projects
None yet
Development

No branches or pull requests

6 participants