U
U
user_of_toster2020-11-20 11:38:50
JavaScript
user_of_toster, 2020-11-20 11:38:50

Why doesn't this refer to the global object when running the script via node?

If you create app.js, add the following:

'use strict';
console.log(this)

and write through the node app console, it will display:
{}

At the same time, if you open the console and manually write the same code, the result will be completely different:
<ref *1> Object [global] {
...
}

If run through node <file name> then this does not refer to the global object. Question - why?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Lynn "Coffee Man", 2020-11-20
@user_of_toster

Because they did it a long time ago and there is no way to fix it.
Inside the module this === module.exports .
DEP0092: Top-level this bound to module.exports
To get global, you need to explicitly refer to it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question