What is the purpose of trying to access this.event on the 5th line? If this.event was a value declared in the parent scope, the fat arrow would actually save you. The implication of using EventEmitter2 would be so that you can access arguments from the callback of the events.on method (ie. args passed inside the callback).
Cofounded Host Collective (DiscountASP.net). Cofounded Player Axis (Social Gaming). Computer Scientist and Technology Evangelist with 20+ years of experience with JavaScript!
So you can use the arrow function to get access to the parent context and still access the element you have attached the listener to... Please don't stop using this just because you don't know how a lib works.
What is the purpose of trying to access
this.eventon the 5th line? Ifthis.eventwas a value declared in the parent scope, the fat arrow would actually save you. The implication of usingEventEmitter2would be so that you can access arguments from the callback of theevents.onmethod (ie. args passed inside the callback).The code is correct. This is how their API is written. I need the
thisfrom thefunctioninside theevents.onmethod, not the parent scope.It works the same way jQuery's
thiscontext works here:You can't write an arrow function for these.
Actually,
jQuerycalls the callback of theelement'sthispassed into it viaapplyandcall. Check it out: code.jquery.com/jquery-3.3.1.jsIn jquerys events
thisis the same asevent.delegateTarget: api.jquery.com/event.delegateTarget/So you can use the arrow function to get access to the parent context and still access the element you have attached the listener to... Please don't stop using
thisjust because you don't know how a lib works.