I referred the documentation Arrow functions do not default this to the window scope, rather they execute in the scope they are created: What does this line mean? At some blogs I read they are defaulted the scope of nearest function or lexical scope. Can someone please elaborate? This is example from the developer.mozilla.org.
window.age = 10; // <-- notice me?
function Person() {
this.age = 42; // <-- notice me?
setTimeout(() => { // <-- Arrow function executing in the "p" (an instance of Person) scope
console.log("this.age", this.age); // yields "42" because the function executes on the Person scope
}, 100);
}
var p = new Person();