Global Object of basic Node. js tutorial, basic node. js tutorial
Global Objects in basic Node. js tutorials
- In browser JavaScript, window is usually a global object.
- The global Object in Node. js is global, and all global variables (except global itself) are attributes of the global object.
- Global serves as the host of global variables.
- Note: Always use var to define variables to avoid the introduction of global variables, because global variables will pollute the namespace and increase the risk of code coupling.
_ Absolute path of the filename script
Indicates the file name of the script being executed. It specifies the absolute path of the output file and is not necessarily the same as the file name specified by the command line parameter. In the module, the returned value is the path of the module File.
console.log(__filename);// C:\Users\admin\main.js
_ Dirname directory of the script
Indicates the directory where the script is being executed.
console.log(__dirname);// C:\Users\admin
SetTimeout (cb, MS) executes the counter function cb
The global function executes the specified function (cb) after a specified number of milliseconds (MS ).
SetTimeout () Only executes the specified function once.
Returns a handle value representing the timer.
Function printHello () {console. log ("Hello, World! ");} // After two seconds, execute the above function setTimeout (printHello, 2000 );
ClearTimeout (t) Stop function t
The clearTimeout (t) global function is used to stop a previously created timer using setTimeout. The parameter t is a timer created by using the setTimeout () function.
Function printHello () {console. log ("Hello, World! ");} // After two seconds, run the preceding function var t = setTimeout (printHello, 2000); clearTimeout (t)
SetInterval (cb, MS) constantly calls function cb
SetInterval (cb, MS) The global function executes the specified function (cb) after the specified number of milliseconds (MS ). Returns a handle value representing the timer. You can use the clearInterval (t) function to clear the timer. The setInterval () method does not stop calling the function until clearInterval () is called or the window is closed. Function printHello () {console. log ("Hello, World! ");} // After two seconds, execute the above setInterval (printHello, 2000) function );
Process a simple interface with the operating system
Process is a global variable, that is, the attribute of the global object.
It is used to describe the status of the current Node. js process and provides a simple interface with the operating system. When you write a local command line program, you need to deal with it.
The above is an example of the global object of Node. js. If you have any questions, please leave a message or go to the community on this site for discussion. Thank you for reading this article and hope to help you. Thank you for your support for this site!