Source: Http://www.w3cschool.cc/nodejs/nodejs-global-object.htmlNode.js Global Object
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.
Console. Log(process. argv);
Save the above code as argv.js and run it with the following command:
$ node Argv. JS 1991 Name=byvoid --< Span class= "PLN" >v "Carbo Kuo" [ ' node ' , '/home/byvoid/argv.js ' ,< Span class= "PLN" > 1991 ' , ' name=byvoid ' Span class= "pun" >, '--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.
Processstdin. Process.. On ( ' data ' , function< Span class= "pun" > (data) { Process.. Write ( ' read from console: ' + Data. });
- 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} Dosomething (functiononend () { 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 (). Rewrite the above program with Process.nexttick ():
functiondosomething ( Args, Callback) { Somethingcomplicated (args. Nexttickcallback} Dosomething (functiononend () { 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.
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. Interested readers can visit http://nodejs.org/api/process.html for more information.
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(' Byvoid%diovyb ',1991);
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:AtObject.<Anonymous> (/home/Byvoid/Consoletrace.Js:1:71)AtModule._compile(Module.Js:441:26)AtObject..Js(Module.Js:459:10)AtModule.Load(Module.Js:348:31)AtFunction._load (module. Js:308:12) at array. 0 (module. Js:479:10) at eventemitter. (node. Js:192:40)
7:node.js Global Objects