What is the difference between class methods, why is this - window?

  • 0
    We have a class:
    class Cls {
      constructor({
        outside
      }) {
        this.outside = outside;
      }
      
      inside = () => {
        console.log('inside', this);
      };
    }


    create instance. in the parameters of which we pass an object with an arrow function:
    const outside = () => {
      console.log('outside', this);
    };
    
    const instance = new Cls({
      outside
    });


    call 2 methods
    instance.inside();         // "inside" instance
    instance.outside();      // "outside" window


    Moreover, the methods in instance, except for console.log, are no different.
    JavaScript Aubrey Gentry, Jun 28, 2019

  • 1 Answers
  • 0
    They differ in the context in which they are declared. They have this different.
    Anonymous

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!