0

Consider the following example:

const f1 = () => this;

function f2() {
    return this;
}

console.log(f1(), f2());

When this code is executed in a browser, the result is as follows:

> window window

But when this code is executed in Node.js, the result is not quite as expected:

> {} global

Why in Node.js the empty object was taken as the context of an arrow function instead of Global Object (global on this platform)?

z1ne2wo
  • 288
  • 1
  • 6

1 Answers1

0

In nodejs, when you use arrow operator, the this operator won't contain the reference object by nature....thatswhy if we wanted to refer any variables using 'this' , we avoid using arrow operators in function definition.

Therefore in your case this is the reason why it's printing empty object.

Amaarrockz
  • 1,613
  • 3
  • 12