The code sample is -
global.a = 'aaa';
const obj = {
a: 'a',
desc() {
console.log(this);
console.log(this.a);
}
}
setTimeout(obj.desc, 2000)
When I run this code in nodejs, I get the following output:
Timeout {
_called: true,
_idleTimeout: 2000,
_idlePrev: null,
_idleNext: null,
_idleStart: 79,
_onTimeout: [Function: desc],
_timerArgs: undefined,
_repeat: null,
_destroyed: false,
[Symbol(asyncId)]: 6,
[Symbol(triggerAsyncId)]: 1 }
undefined
But the same code, with global
changed to window
in Chrome/Firefox prints aaa
and the window
object, which is what this MDN doc says and which is what I expect.
I was under the impression that nodejs and Chrome both use Google's v8 JS engine to execute JavaScript. So why is the output different? Is there something more to this? I tried searching but couldn't find satisfactory answers.
Node version - v9.10.
Chrome's version - Version 70.0.3538.110