My goal is that I'm trying to pass the this context from a normal function to an arrow function but I keep getting undefined.
I know that this in a normal function behaves dynamically and determined by how the function was called. The this inside an arrow function follows lexical scoping rules to determine its value. However, when I call printWrapper() on line 16, this is set to the car object, so when I further call printThis(), as per the lexical scoping rules, it should print the car object, but in my case the this object being printed on line 2 is the global object.
printThis = () => {
console.log(this); // prints the global object
}
var car = {
year: 2015,
printThis: printThis,
printWrapper: printWrapper
}
function printWrapper() {
console.log(this); // prints the car object
printThis();
}
car.printWrapper();