What is lexical 'this'? [duplicate]

Could someone please give me a brief introduction to lexical this?

"An arrow function expression (also known as fat arrow function) has a shorter syntax compared to function expressions and lexically binds the this value (does not bind its own this, arguments, super, or new.target). Arrow functions are always anonymous."

Does it mean that when I call a function member using the 'this' ref from a 'Fat Arrow' Function the 'this' always refers to the enclosing 'this'?


Solution 1:

You seem to have the correct understanding of what happens with this in an arrow function. I will offer an explanation that I think adds to the conversation and hopefully solidifies your understanding.

As you probably know, when you define a function and use a variable inside of it, it checks if the variable has been defined in its scope. If it is, it uses it! If not, it checks the enclosing scope for that variable definition. It keeps checking enclosing scopes until it finds the variable or reaches global scope. Now, function definitions that are not arrow functions define this for you, implicitly. Thus, they will never check an enclosing scope when you try to use this in their scope (because they find it in their own scope!). Arrow functions do NOT define their own this, so they go to the enclosing scope and look for it just as they would with any variable you try to use in their scope.

Solution 2:

As a way to describe the behavior of this in arrow functions, the term "lexical this" is somewhere between confusing and wrong.

The behavior is quite simply is that this in an arrow function refers to the this in the surrounding function--or perhaps it would be simpler and more accurate to just say that the arrow function does not define (or "bind") its own this.

The unfortunately terminology of "lexical this" may have been perpetuated by a poor choice of wording in the MDN article on arrow functions (which until recently also encouraged people to use the obsolete term "fat arrow function"). This has now been cleaned up. Note also that the spec never uses the term "lexical" in relation to this in arrow functions. I'd be curious to know who came up with this expression.

Solution 3:

Lets say you have a click listener. In that listener you are performing some AJAX operation like setTimeout. After the set time has been reached, the code inside the callback will be executed. Inside that callback you may have accessed this to change the color of the clicked button. But the control will be out of context due to AJAX operation. ES2015 introduced the arrow function to fix that problem. The arrow function captures the this value of the enclosing context.

The example use-case is:

$('.btn').click(function () {
    setTimeout(function () {
        $(this).text('new'); 
        // This will cause an error since function() defines this as the global object.
    } ,100); 
}); 

To avoid this case:

$('.btn').click(function () { // <- Enclosing context
    setTimeout( () => {
        $(this).text('new')  } 
        // This works, because this will be set to a value captured from the enclosing context.
      ,100);
});