Why is this undefined in a function defined and called in a JS class method. I exepected this to be window

Viewed 292

I thought I understood the mechanism of this in JS but -> I can't explain why if you define and call a function in the method of a JS Class (syntax Class), this value of 'this' in this function is undefined when I expected it to be window.

Not included in the snipet but I tried to check if the context of the method was executed in strict mode and it was not.

I tried the code in js fiddle and on my chrome browser console.

class ComponentClass {
    classMethod() {
      function funcInMethod() {
        console.log('this in in the function funcInMethod defined and called from the method class: ', this);
      }      
      funcInMethod();
    }
 }
 
var classInstance = new ComponentClass(); 
classInstance.classMethod();

// logged: > undefined
// expected: > window

1 Answers
Related