If using declared in header of for-of loop variable in closure, placed in this loop, keyword "this" in that closure becomes equal to global object.
var servant = {
sayNumbers() {
for (let number of [1, 2, 3]) {
(() => {
number; //without number reference "this" is right
console.log(this); //window
})();
}
}
}
servant.sayNumbers();
If using declared in header of for-of loop variable in closure, placed in this loop, keyword "this" in that closure becomes equal to global object.
This code is compiled into
And all works fine if function, closing "number" variable, is called via "call" method with definition of "this".