node. JS: Global objects

Source: Internet
Author: User

Summary : This blog mainly describes the global objects in node. js.

In 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. such as:console,process.

1. Global objects and global variables

The fundamental role of global is to be the host of variables. The following conditions are met:

Variables defined at the outermost layer;

The properties of the global object;

Implicitly-defined variables (variables that are not directly assigned) are defined.

2.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. Some of the most common member methods of the Process object are described below.

  ▲ process.argv: An array of command-line arguments, the first element is node, the second element is the script file name, and the third element starts with each element being a run parameter.

Console.log (PROCESS.ARGV);

Save the above code as argv.js and run it from the following command line:

$ node Argv.js 1991 name=byvoid--v "Carbo Kuo"[       ' node ',    '/home/byvoid/argv.js ',    ' 1991 ',    ' name=byvoid ',    '--v ',    ' Carbo Kuo ']

  ▲ process.stdout: Standard output stream, usually we use Console.log () to print characters to the standard output, while the Process.stdout.write () function provides a lower level interface.

  ▲ process.stdin: standard input stream, when initially it is paused, want to read data from the standard input, you must resume the stream and manually write the stream's event response function.

process.stdin.resume ();p rocess.stdin.on (' data ',function(data) {    Process.stdout.write ( ' read from console: ' + data.tostring ();    });

  ▲process.nexttick (callback): sets a task for the event loop, and node. JS calls callback the next time the event loops over the response.

The node. JS process has only one thread, so there is only one event executing at any moment. If this event consumes a lot of CPU time. Process.nexttick () provides a tool that can break up complex work and turn it into a smaller event.

function dosomething (args, callback) {    somethingcomplicated (args);    Callback ();} DoSomething (function  onEnd () {    compute ();})

We assume that compute () and somethingcomplicated () are two time-consuming functions, and the above program executes somethingcomplicated () first when calling DoSomething (), and then immediately invokes the callback function. The compute () is also executed in OnEnd (). Use Process.nexttick () below to rewrite the above program:

function dosomething (args,callback) {    somethingcomplicated (args);    Process.nexttick (callback);} DoSomething (function  onEnd () {    compute ();})

The rewritten program splits the time-consuming operation into two events, reducing the execution times of each event and increasing the response speed of the event.

Warning: Do not use settimeout (fn,0) instead of Process.nexttick (callback), which is much less efficient than the latter. In addition, the process also shows methods such as Process.platform, Process.pid, Process.execpath, Process.memoryusage (), and POSIX process signal response mechanisms.

3.console

Console is used to provide console standard output.

  ▲console.log (): Prints characters to the standard output stream and ends with line breaks. Console.log () takes a number of parameters, and if there is only one argument, outputs the string form of the parameter. If there are multiple parameters, it is output in a format similar to the C-language printf () command. The first argument is a string, and if there are no arguments, only one line break is printed.

Console.log (' Hello World '); Console.log (' byvoid%diovyb '); Console.log (' Byvoid%diovyb ', 1991);

The result of the operation is:

 ▲console.error (): Same as Console.log () usage, just output to standard error stream.

  ▲console.trace (): Outputs the current call stack to the standard error stream.

Console.trace ();

The result of the operation is:

node. JS: Global objects

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.