Global Object of basic Node. js tutorial, basic node. js tutorial

Source: Internet
Author: User

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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.