1

I want to make an event handler with setters and getters, but there is a problem. The this keyword is not working in my function inside the object correctly. I was looking everywhere, but I couldn't find the problem.

const Event = {
    listen: ()=>{
        console.log(this);
    }
};
Event.listen();

This is a part of my code which returns me: Window {window: Window, self: Window, document: document, name: '', location: Location, …}.

I tryed searching on google, documentation (mozzila), stackoverflow. I tryed deleting all parts of code to have only this object with this funciton.

I am excepting, that the this will return the Event{listen:function} object.

Thanks for your help.

Tomas
  • 11
  • 1
  • I'm just wondering what have you actually searched for, this is not too hard to find from [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions#cannot_be_used_as_methods) either. – Teemu Feb 19 '23 at 10:34

0 Answers0