Instance Analysis of Global Variables in nodejs
- 2021-07-26 06:55:37
- OfStack
1.global
Similar to window in the client javascript running environment
module1.js:
module.exports={};
// The shameful use of global variables
global.varA = "abc";
With regard to the global object, global can be omitted in actual use. It is the default root scope, which is equivalent to the window object in the web environment.
The global object has several useful properties:
console.log(__dirname);// Current path
console.log(__filename);// Currently executing js File path
//global.process Gets the current Process Instances
2.process
Obtain the current Node process information, which is generally used to obtain information such as environment variables
process.on('exit', function(code) {
// The following code will never be executed
setTimeout(function() {
console.log(" The code does not execute ");
}, 0);
console.log(' The exit code is :', code);
});
console.log(" End of program execution ");
3.console
Input and output
main.js:
var c = require("./calculator");
console.log(global.varA);// Use global Object accesses to the " Global " Variable