this is a complicated concept to grasp and I'm sure there is room for improvement in its design; however, it is not broken and especially not broken to the point where we need wrapper libraries to unbind contexts. Arrow functions were created to pass the parent context (this of the parent) into child scopes.
Cofounded Host Collective (DiscountASP.net). Cofounded Player Axis (Social Gaming). Computer Scientist and Technology Evangelist with 20+ years of experience with JavaScript!
I do not know a single developer that hasn't had to debug a this problem by writing console.log(this). It is not accepted that as a JavaScript developer, at some point you will have to debug `this.
Duh! Ofcourse you will. Just like with any other code that you're not sure about YET. Then as you debug it to get an insight, to see what this points to you'll ask yourself - wait a second how did that happened? Does that mean... OOOH! And then the spoon bends, the matrix code appears and you get it. And that my friend is one of the big pleasures of working with your head.
It aint broken. Case closed.
Cofounded Host Collective (DiscountASP.net). Cofounded Player Axis (Social Gaming). Computer Scientist and Technology Evangelist with 20+ years of experience with JavaScript!
Cofounded Host Collective (DiscountASP.net). Cofounded Player Axis (Social Gaming). Computer Scientist and Technology Evangelist with 20+ years of experience with JavaScript!
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.
this
is a complicated concept to grasp and I'm sure there is room for improvement in its design; however, it is not broken and especially not broken to the point where we need wrapper libraries to unbind contexts. Arrow functions were created to pass the parent context (this
of the parent) into child scopes."Having difficulties" is part of learning process. That's how you grow.
I do not know a single developer that hasn't had to debug a
this
problem by writingconsole.log(this)
. It is not accepted that as a JavaScript developer, at some point you will have to debug `this.it doesn't have to be
Duh! Ofcourse you will. Just like with any other code that you're not sure about YET. Then as you debug it to get an insight, to see what
this
points to you'll ask yourself - wait a second how did that happened? Does that mean... OOOH! And then the spoon bends, the matrix code appears and you get it. And that my friend is one of the big pleasures of working with your head.It aint broken. Case closed.
Just FYI, saying case closed doesn't close the case.
You don't have to use the library. But please remember to think of me the next time you write
console.log(this)
.Arrow functions solve ONE of the problems with
this
.Here's a reference to
this
that you can't arrow function your way out of.nothis
also does more than removethis
. It lets you use arrow functions and also argument destructuring. Both of which are not options otherwise.What is the purpose of trying to access
this.event
on the 5th line? Ifthis.event
was a value declared in the parent scope, the fat arrow would actually save you. The implication of usingEventEmitter2
would be so that you can access arguments from the callback of theevents.on
method (ie. args passed inside the callback).The code is correct. This is how their API is written. I need the
this
from thefunction
inside theevents.on
method, not the parent scope.It works the same way jQuery's
this
context works here:You can't write an arrow function for these.
Actually,
jQuery
calls the callback of theelement
'sthis
passed into it viaapply
andcall
. Check it out: code.jquery.com/jquery-3.3.1.jsIn jquerys events
this
is 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
this
just because you don't know how a lib works.