0

in nodejs REPL

$ node
> var a = 10;
undefined
> console.log(this.a);
10

in execute > node test.js

// test.js
var a = 20;
console.log(this.a);
$ node test.js
undefined

What is different this context from 'repl vs execute js file'?

in execute, console.log(this) -> just {}

not global context

황다슬
  • 1
  • 1
  • There is also specifically [this answer](https://stackoverflow.com/a/52193294/3689450) which I think has the best description. Basically, `this !== global` when you are executing a file. When you do, `this` is the *module* for that file, while in the REPL context `this` is the global object. – VLAZ Jan 29 '20 at 08:44
  • @VLAZ wow! thank you! – 황다슬 Jan 29 '20 at 12:01

0 Answers0