Here this keyword which is in arrow function points to obj's variable environment
var greeting = 'hi';
const obj = {
greeting: 'hey',
fo() {
const greeting = 'hola';
const arrowFo = () => {
console.log(this.greeting);
};
arrowFo();
},
};
obj.fo(); //logs: hey
var greeting = 'hi';
const obj = {
greeting: 'hey',
fo() {
const greeting = 'hola';
const fo2 = function () {
const greeting = 'hello';
const arrowFo = () => {
console.log(this.greeting);
};
arrowFo();
};
fo2();
},
};
obj.fo(); //logs: hi
thiskeyword never points to an "environment", it points to a value (typically an object). It refers to thethisvalue info2, which is the global object since you calledfo2()(not as a method).thisinsidefoo2refers to thewindowobject and not theobjbecause of the way you have calledfo2. Replacefo2()withfo2.call(this);and you will see the expected output.hellobutheywhich is present onobjsince nowthiswill beobj. I think the asker is confusingconst/let/vardeclared variables being accessed viathis.