I have a question about this in javascript.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this
When I read this article, it said
In arrow functions,
thisretains the value of the enclosing lexical context's this. In global code, it will be set to the global object
But for example, the code below
const person = {
hobby: "loafing around",
sayHobby: function() {
setTimeout(() => {
console.log(`My hobby is ${this.hobby}`);
}, 100);
}
};
person.sayHobby()
In this example, Arrow function callback in setTimeout was enclosed by setTimeout function. so I think this it should be global(window) because setTimeout is defined in gloabal object.
I know my thinking is wrong, but don't know how to understand the meaning of enclosing lexical context. Could you please help me to understand this ? How to find lexical context ?