node. JS Global Object

Source: Internet
Author: User
Tags stdin

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.

We have direct access to objects in node. js that are typically global properties, such as console, process, and so on, as described below.

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.

PROCESS.ARGV is 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.

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

$ node Argv.js 1991 name=byvoid--v "Carbo Kuo"' node '/home/byvoid/argv.js  ' 1991 ' ' Name=byvoid'--v' Carbo Kuo ']
      • Process.stdout is the standard output stream, which is usually the console.log () that we use to print characters to the standard output, while the Process.stdout.write () function provides a lower-level interface.
      • Process.stdin is the standard input stream, initially paused, and to read from the standard input, you must resume the stream and manually write the stream's event response function.
Process.stdin.resume (); Process.stdin.on (function(data) {Process.stdout.write (' Read From console: ' +
    • The function of Process.nexttick (callback) is to set a task for the event loop, and node. JS calls callback the next time the event loops over the response.

Beginners are likely not to understand the function, what tasks can not be completed in the present, need to be handed over to the next event loop response to do it?

We have discussed that node. js is suitable for I/O intensive applications, not compute-intensive applications, because a node. JS process has only one thread, so there is only one event executing at any moment.

If this event takes up a significant amount of CPU time, the next event in the event loop will have to wait long, so one of the programming principles of node. JS is to minimize the execution time of each event. Process.nexttick () provides a tool to break up complex work and turn it into smaller events.

Functiondosomething (args, callback) {     somethingcomplicated (args);     Callback (); } dosomething (Functiononend () {     

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, Compute () is also executed in OnEnd (). Use Process.nexttick () below to rewrite the above program:

Functiondosomething (args, callback) {     somethingcomplicated (args);     Process.nexttick (callback); } dosomething (Functiononend () {     

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.

Note: do not use settimeout (fn,0) instead of Process.nexttick (callback), which is much less efficient than the latter.

We have explored several common members of the process object, and in addition the process shows the methods of Process.platform, Process.pid, Process.execpath, Process.memoryusage (), And the POSIX process signal response mechanism.

Console

Console is used to provide console standard output, which is a debugging tool provided by the JScript engine of Internet Explorer, and later becomes a de facto standard for browsers.

node. JS uses this standard to provide a console object that is consistent with customary behavior to output characters to the standard output stream (STDOUT) or standard error stream (stderr). Console.log (): Prints characters to the standard output stream and ends with line breaks.

Console.log accepts several parameters, and if there is only one argument, it 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 (

The result of the operation is:

Hello World byvoid%
    • 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:

trace:at Object. <anonymous> (/home/byvoid/consoletrace.js:1:71) at Module._compile (module.js:441:26) at Object. JS (module.js:459:10) at Module.load (module.js:348:31) at Function._load (module.js:308:12 ) at Array. 0 (module.js:479:10) at Eventemitter._tickcallback (node. js:192:40)

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.