0

Consider this code:

var obj = {
   method : function(){
      console.log( this ); // This prints the **obj** correctly
   }
};

And the same code with Lambda:

var obj = {
   method : () => {
        console.log( this ); // This prints **Window** object
    };
};

Why are the outputs different?

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
madhairsilence
  • 3,787
  • 2
  • 35
  • 76

1 Answers1

0

The ES6 arrow function syntax uses “lexical scoping” to figure out what the value of “this” should be. Lexical scoping is fancy way of saying it uses “this” from the surrounding code… the code that contains the code in question, hence it is

window

here.

Manishz90
  • 2,534
  • 1
  • 12
  • 11
  • 1
    It's a mystery what value you see in regurgitating rudimentary information about arrow functions which is documented in thousands of pages of documentation, intros, tutorials, blog posts, specs, and SO questions. –  Aug 16 '17 at 06:33