0

Consider the following node app:

console.log('default this:', this);

function f() {
    console.log('this inside f:', this);
}

f();

here is the output (in node v14.17.4 on macOS):

default this: {}

this inside f: <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]
  }
}

Why is this set to {} outside the function but the global object inside it?

d512
  • 29,226
  • 23
  • 72
  • 100

0 Answers0