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?