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'?
The answer to your query is Yes. As you have already stated fat arrow functions don't have a this
reference so they use the reference of the enclosing context, which in turn, gives us a chance to control how the call to this
inside a fat arrow function would respond.
For e.g.:
Having fat arrow function inside an object which makes use of this
would look for outer context. In this case, the context is window
:
var foo = {
bar: () => this.baz // 'this' is window
}
However, if you use the ES6 syntax which is public class field syntax:
class MyClass {
handleEvent = () => {
this.letsCode(); // 'this' is instance of MyClass
}
}
There are many other instances (like using in render
function of React on an HTML element to set click listeners) which are out of the scope of this question where fat arrow functions are used and this
is appropriately bound to the context in which it is used thus giving us the facility to control its nature. Hope it helps!