Node: Why do you bind ”this“ in event handler

Created on 17 Mar 2019  Â·  5Comments  Â·  Source: nodejs/node

https://github.com/nodejs/node/blob/2f9c9eebb43696bda0467f119951b5914a348994/lib/events.js#L198
Why do you bind ”this“ here?It is not compatible with browser.

events question

Most helpful comment

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.

All 5 comments

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.

it’s a best way when you extend EventEmitter

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

Seems like not an issue? Closing...

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

seishun picture seishun  Â·  3Comments

addaleax picture addaleax  Â·  3Comments

sandeepks1 picture sandeepks1  Â·  3Comments

stevenvachon picture stevenvachon  Â·  3Comments

danielstaleiny picture danielstaleiny  Â·  3Comments