While I was testing what "this" was referring to at different phases in below code, I expected the last output to be the content of global, yet it showed {} instead.
var hero = {
name: 'Monkey',
sayMyName: function() {
console.log(this.name);
}
};
hero.sayMyName(); // A'Monkey'
var speakOut = hero.sayMyName;
speakOut(); // B global
const someone = { name: 'passenger' }
hero.sayMyName.call(someone); // C'passenger'
function here() {
console.log(this);
}
const there = () => {
console.log(this);
}
here(); // D global
there(); // E global
output
monkey
undefined
passenger
<ref *1> Object [global] {
global: [Circular *1],
clearInterval: [Function: clearInterval],
clearTimeout: [Function: clearTimeout],
setInterval: [Function: setInterval],
setTimeout: [Function: setTimeout] {
[Symbol(nodejs.util.promisify.custom)]: [Getter]
},
queueMicrotask: [Function: queueMicrotask],
clearImmediate: [Function: clearImmediate],
setImmediate: [Function: setImmediate] {
[Symbol(nodejs.util.promisify.custom)]: [Getter]
}
}
{}
Does anyone know why?