Global objects and global variables for node. js

Source: Internet
Author: User

1. Global objects

All modules can be called

1) Global: Indicates that node is in an environment similar to the Window object in the browser.

2) Process: points to Node's built-in process module, allowing developers to interact with the current processes.

For example, if you enter node directly in a DOS or terminal window, you will enter node's command line (REPL environment). Exit to exit, you can enter Process.exit ();

3) Console: Point to Node's built-in console module, which provides standard input and standard output functions in the command line environment.

is usually written console.log (), needless to say

2. Global functions:

1) Timer function: A total of 4, respectively, is settimeout (), cleartimeout (), SetInterval (), Clearinterval ().
2) require: for loading modules. The King's house in the common See, Tri Yutong several times before the smell.

3. Global variables:

1) _filename: Point to the currently running script file name.

2) _dirname: Point to the directory where the currently running script resides.

4, quasi-global variables

Local variables within the module, pointing to different objects depending on the module, but all modules are applicable, can be regarded as pseudo-global variables, mainly module, module.exports, exports and so on.


The module variable refers to the current block. The module.exports variable represents the external output interface of the current module, and the other file loads the module, in effect reading the module.exports variable.

The identifier of the Module.id module, usually the file name of the module.
The file name of the Module.filename module.
Module.loaded returns a Boolean value that indicates whether the module has finished loading.
Module.parent returns the module that uses the module.

Module.children returns an array that represents the other modules to be used by the module.

It should be noted here that the exports variable is actually a link to the Module.exports object, which is equivalent to a command on each module's head and one row.

var exports = Module.exports;

As a result, you can add methods to the exports object when the external output module interfaces, but you cannot directly point the exports variable to a function:

Exports = function (x) {console.log (x);};

The above notation is invalid because it cuts off the link between exports and module.exports. However, it is possible to write the following.

[JavaScript]View Plaincopy
    1. Exports.area = function (r) {
    2. return Math.PI * R * r;
    3. };
    4. Exports.circumference = function (r) {
    5. return 2 * Math.PI * r;
    6. };

If you feel that the difference between exports and module.exports is difficult to distinguish, a simple way to do this is to abandon the use of exports, using only module.exports.

Reference article:

Http://javascript.ruanyifeng.com/nodejs/basic.html#toc5

Global objects and global variables for node. js

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.