Based on my knowledge of JavaScript and let, a variable defined with this keyword is available after it has been defined throughout an entire scope (not necessarily function execution context like var).
Quoting from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let
"Variables declared by let have their scope in the block for which they are defined, as well as in any contained sub-blocks."
It is also my understanding that directly within a method (member function), this refers to the object, but within all other functions, this refers to the global object. Given this, I am confused by the result below:
let a = 7;
let b = {
a: 13,
foo: function() {
(function() {
console.log(this.a);
})();
}
};
b.foo();
I would have expected the value 7. Instead, I get undefined on Chrome. On Firefox it seems nothing is logged.
I get the same behaviour if I replace the IIFE with a call to a function defined only within foo. Can anyone clarify what has happened here? Is that IIFE not part of a contained sub-block?