‘this’ different between REPL and script

Node’s REPL is global. Code from a file is in a “module”, which is really just a function.

Your code file turns into something like this very simplified example:

var ctx = {};
(function(exports) {
    // your code
    console.log(this === global);
}).call(ctx, ctx);

Notice that it’s executed using .call(), and the this value is set to a pre-defined object.

Leave a Comment