0

Example 1

const obj = {
  perimeter: () => {
    console.log(this); // this points to window
  }
}

obj.perimeter()

const obj = {
    sing(){
    console.log(this);
    const b = () =>{
        console.log(this); // this points to obj
    }
    b();
  }
}

obj.sing()

Arrow function this points lexical scope why the first output is different?

Arrow function this points lexical scope why the first output is different?

  • 1
    in the second example, the lexical scope is the `sing` method, which has a `this` of `obj` when called (as you do) as `obj.sing()`. In the first example, the surrounding scope is the global scope, so that's why you get the global `window` object as `this`. – Robin Zigmond Jul 15 '23 at 16:06
  • Got it thanks @RobinZigmond – Indrajeet Jul 15 '23 at 16:19

1 Answers1

-2

because the arrow function as no this context. JavaScript Arrow Function

Deniz
  • 1,435
  • 14
  • 29