Monday, May 20, 2024
106
rated 0 times [  108] [ 2]  / answers: 1 / hits: 19344  / 9 Years ago, sat, january 9, 2016, 12:00:00

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'?


More From » ecmascript-6

 Answers
64

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.


[#63788] Thursday, January 7, 2016, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
tylerdamiena

Total Points: 139
Total Questions: 90
Total Answers: 118

Location: Liechtenstein
Member since Wed, Dec 8, 2021
3 Years ago
tylerdamiena questions
;