this is my code
var a=1;
console.log(global.a);
console.log(this.a);
both print undefined. And it indicates that a doesn't belong to either global or this (current module).
I want to know which object a var belongs to.
To understand this you need to know about the Module Wrapper in Node.js.
All the JavaScript code run by Node.js is not run directly, instead wrapped by a function call which is called by Node internally.
The Module Wrapper:
(function (exports, require, module, __filename, __dirname) {
// You code goes here
});
The code which actually runs is:
(function (exports, require, module, __filename, __dirname) {
var a=1;
console.log(global.a);
console.log(this.a);
});
So the var a is inside the scope of an anonymous function not part of the global object also not part of this(Since this function is not called with any object i.e Function.prototype.call(null) )
Refer The module wrapper
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With