0

I know there are questions that look like this one but my context looks much simpler and strange:

I have this object:

        const obj = {
            fn: () => {
                console.log('obj', this);
            }
        }
        obj.fn();

And it's logging Window object instead on obj.

It never happened to me before, looks so strange, has there been any change in ECMAScript and arrow functions in the last months? Guessing not but you never know... Makes no sense to me but I may be missing something...

theredled
  • 998
  • 9
  • 20
  • Arrow functions never had a `this` context since they were introduced. Nothing changed. – kelsny Mar 20 '23 at 20:16
  • Holy cow, haven't worked for 6 month and I have already forgotten that... Thanks you – theredled Mar 20 '23 at 20:25
  • This appears to have been answered before https://stackoverflow.com/questions/15831509/javascript-this-refers-to-window-instead-of-object-inside-function – ChrisSc Mar 20 '23 at 20:29
  • @ChrisSc not the same, the guy is calling a function defined inside another function, and it has no arrow functions. – theredled Mar 20 '23 at 20:30

1 Answers1

-1
const obj = {
   fn() {
      console.log('obj', this)
   }
}
obj.fn();

output: obj Object { fn: fn() }

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions