node. JS Global Object

Source: Internet
Author: User

There is a special object in JavaScript called a global object, and all of its properties can be accessed anywhere in the program, that is, global variables.

In browser JavaScript, the window is usually a global object, while the global object in node. JS is global, and all global variables (except the global itself) are properties of the global object.

In node. js, we can directly access the properties of global without having to include it in the app.

Global objects and global variables

The fundamental role of global is to be the host of variables. As defined by ECMAScript, variables that meet the following conditions are global variables:

    • Variables defined at the outermost layer;
    • The properties of the global object;
    • Implicitly-defined variables (variables that are not directly assigned) are defined.

When you define a global variable, the variable also becomes a property of the global object, and vice versa. It is important to note that in node. js You cannot define variables at the outermost layer because all user code belongs to the current module, and the module itself is not the outermost context.

Note: always use var to define variables to avoid introducing global variables, because global variables pollute namespaces and increase the risk of code coupling.

Process

Process is a global variable, which is a property of the global object.

It is used to describe the object of the current node. JS process State and provides a simple interface to the operating system. Usually when you write a local command-line program, you have to deal with it. Some of the most common member methods of the Process object are described below.

Serial Number Events & Descriptions
1 Exit
Triggered when the process is ready to exit.
2 Beforeexit
This event is triggered when node clears the event loop and there is no other arrangement. In general, node exits when no process is scheduled, but the ' beforeexit ' listener can be called asynchronously so that node continues execution.
3 Uncaughtexception
This event is triggered when an exception bubbles back into the event loop. If you add a monitor to an exception, the default action (print stack trace information and exit) does not occur.
4 Signal Events
Triggered when the process receives a signal. The list of signals is described in the standard POSIX signal names, such as SIGINT, SIGUSR1, etc.
Instance

To create the file main.js, the code looks like this:

function (code) {  //  The following code will never execute  setTimeout () (function() {    Console.log ("The code will not execute")  ; 0);    Console.log (' exit code: ', Code);}); Console.log ("End of program execution");

Execute the main.js file, as shown in the following code:

Node main. JS

End of program execution exit codes are: 0

node. JS Global Object

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.