Node. js Lesson 7 (Global Objects and global variables)

Source: Internet
Author: User

Zookeeper

Concept: All attributes can be accessed anywhere in the program, that is, global variables. In JavaScript, window is a global object, while the global Object of Node. js is global,
All global variables are attributes of the global object, such as console and process.
1. Global Objects and global variables
Global serves as the host of global variables. The following conditions are met to become a global variable:
1. variables defined at the outermost layer
2. Attributes of Global Objects
3. Implicitly Defined variables (undefined variables directly assigned values)
In Node. js, variables cannot be defined at the outermost layer, because all user code belongs to the current module, and the module itself is not the outermost layer context.
Ii. process
It is used to describe the status of the current Node. js process. Provides a simple interface with the operating system, which is usually used when writing a local command line program.
1. process. argv is a command line parameter array. The first element is node, the second element is the script file name, and the third element starts. Each element is a running parameter.
2. process. stdout is a standard output stream. The console. log () is usually implemented using process. stdout. write (); At the underlying layer.

// Process. stdout. write ("octopus ");
3. process. stdin is a standard input stream, which is paused at the beginning. To read data from a standard input stream, you must restore the stream and manually write the corresponding streaming event function.

// Resume the stream

Process. stdin. resume ();

Process. stdin. on ('data', function (data ){

Process. stdout. write ("read from console" + data. toString ());

});
4. The process. nextTick (callback) function is to set a task for the event loop. Node. js will call callback when the next event is called cyclically.
Node. js is suitable for IO-intensive applications, rather than computing-intensive applications. Process. nextTick () provides such a tool to break up complicated work,

Small events.

Function doSomething (args, callback ){
SomethingComplited (args );
Callback ();
}
DoSomething ('20140901', function onEnd (){
Compute ();
})
Assume that compute () and somethingComplited () are two time-consuming functions. When the above program calls doSomething, it will first execute somethingComplited (args) and then immediately call the callback function,
In onEnd (), compute () is executed again and rewritten:


Function doSomething (args, callback ){
SomethingComplited (args );
Process. nextTick (callback );
}
After process. nextTick () is used, the rewritten program splits the time-consuming operation into two events to reduce the execution time of each event and increase the event speed.

5. Other methods of process: process.platform()、process.pid(,、process.exe cPath (), process. memoryUsage () and so on...
Http://nodejs.org/api/process.html

Iii. console
Logger logger = LoggerManager. getLogger (*. class. getName () of log4j in java ());
Logger.info
Logger. warn
Logger. error logger. fatal
FATAL: Used in extreme situations, that is, situations that require immediate attention. Errors of this level usually need to trigger the pager of the O & M engineer.

ERROR: Displays an error or a common error, but does not suspend the system. Errors of this degree are generally triggered by sending an email,

Send the message to the alert list. The O & M personnel can record this bug in the document and submit it.

WARN: Not necessarily a bug, but some may want to know this situation. If someone is reading the log file, they usually want to read any warning from the system.

INFO: Used for basic and high-level diagnostic information. Messages should be generated when long-running code segments start and end to know what the system is doing.

However, such information should not be too frequent.

DEBUG: Used to assist in low-level debugging.


The console is used to provide standard output of the console. Node. js follows this standard and provides console objects that are always used to behavior.
1. console. log (). Print characters to the standard output stream and end with a line break.
Use Case:
Console. log ("hello ");
Console. log ("hello % octopus ");
Console. log ("hello % octopus", "Mr .");
2. The usage of console. error () is the same as that of console. log (), but is output to the standard error stream.
3. console. trace (); outputs the current call stack to the standard error stream


Related Article

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.