Hello, there is a question, there is such a code.

var obj = { num: 10, method: function(){ function test(){ console.log(this.num); } test(); } } obj.method(); // undefined 

It will return undefinded because the context of the function call test is global. But if we first declare the variable num and assign a value to it. This will output the result of the global context.

 var num = 11; var obj = { num: 10, method: function(){ function test(){ console.log(this.num); } test() } } obj.method(); //11 

Checked in the node and the browser and it works. But in the node it works only in the REPL mode, and if I feed it a script from a file, it returns the result undefined, can you please tell why this is happening?

  • this in this case is global when you load it from a file it is considered a module, num is declared inside the module and this.num is undefined because the variable is declared in the module and not in the global. In the reple, it works as the top level is not a module but a global and the variable num is declared directly in it and is available via this.num - Grundy
  • @Grundy it should have been the answer - Pavel Mayorov
  • @PavelMayorov, aha, yesterday wrote in a quick way :-) need to think about how to make it better, is this not a duplicate of the question about the loss of context? - Grundy
  • @Grundy is definitely not. Here the author understands the mechanism of context loss, but did not understand the mechanism of modules in the node. - Pavel Mayorov
  • @PavelMayorov, added the answer - Grundy

1 answer 1

The difference of nodejs from the browser is that it already supports modules in javascript.

An important difference between modules is that variables declared with var globally for a module are not added to the global object, but to the module.

Any file is regarded as a module, so when using the same code in the REPL and loading from the file, you can get unexpected results.

In the above code, it can be noted that this inside the test function points to a global object (window in the browser, global in node).

When the code was executed in the browser or REPL, the num variable was added to the global object, and its value could be obtained via this

When loading from the file, the variable is added to the module, and not to the global object, so its value could not be obtained through this .

  • So kind of like this is normal behavior. When you declare var, you declare it in the current context, and if without var, the variable will be created globally, this is why it is recommended not to declare children in this way, and it seems like use strict explicitly prohibits doing so - pnp2000
  • @ vnn198, a simple example, by loading a file with the string var num=11 into a browser — a field will be added to the window, but not to the node. That was the problem of the author of the question. - Grundy
  • well, if in the second example you remove var from the first line, then everything works, although it is impossible to declare variables as good for the node. - pnp2000