DEV Community

Discussion on: Rethinking JavaScript: The complete elimination and eradication of JavaScript's this.

joelnet profile image
JavaScript Joel Author

The code is correct. This is how their API is written. I need the this from the function inside the events.on method, not the parent scope.

It works the same way jQuery's this context works here:

$('p').on('click', function() {

You can't write an arrow function for these.

Thread Thread
zeddotes profile image

Actually, jQuery calls the callback of the element's this passed into it via apply and call. Check it out:

Thread Thread
benwick profile image

In jquerys events this is the same as 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.

$('p').on('click', (evt) => {
Enter fullscreen mode Exit fullscreen mode