node. JS Learning Note 5--Core Module 1

Source: Internet
Author: User

The main contents of node. JS Core module include: (1) Global Objects (2) common tools (3) event mechanism (4) File system Access (5) HTTP Server and client

One: Global objects

The global object in node. JS is globals, and all global variables (except the global itself) are properties of global.

The fundamental role of global is to be the host of variables.

Conditions for global variables:

(1) The variables defined at the outermost layer;

(2) The properties of the global object;

(3) Implicitly defined variables (variables that are not directly assigned are undefined (notavailable in strict mode ))

It is not possible to define variables at the outermost layer in node. js because all user code belongs to the current module, and the module itself is not the outermost context.

I. 1 process

Process is a global variable, a property of the global object that is used to describe an object that describes the state of the current node. JS process, providing a simple interface to the operating system.

(1) 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.

(2) process.stdout is the standard output stream, Console.log () prints characters to the standard output, while the Process.stdout.write () function provides a lower level interface.

(3) The Process.stdin is the standard input stream, initially paused, to read from the standard input, the stream must be restored, and the event response function of the stream is written manually.

Process.stdin.resume ();

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

Process.stdout.write (' read from console: ' + data.tostring ());

});

(4) Process.nexttick The function of (callback) is to set a task for the event loop, and node. JS will call callback the next time the event loops over the response.

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

API Reference for process: http://nodejs.org/api/process.html

I. 2 Console (the so-called various streams: related to the operating system settings, sometimes the operating system will put the error output and normal output of the program in different places, such as different log files, different terminal)

Console is used to provide console standard output for outputting characters to the standard output stream (STDOUT) or standard error stream (stderr).

(1) Console.log accepts a number of parameters, if there is only one argument, outputs the string form of the parameter, or, if there are multiple parameters, output in the format of the printf () command similar to the C language.

(2) Console.error (): Consistent with console.log usage, just output to the standard error stream.

(3) Console.trace (): Outputs the current call stack to the standard error stream.

Two: Common tools util

Util is the core module of node. JS and provides a collection of commonly used functions to compensate for the lack of the functionality of core JavaScript too thinly.

Two. 1 util.inherits

Util.inherits (constructor: constructor, Superconstructor: The parent constructor) is a function that implements the prototype inheritance between objects. JavaScript's object-oriented is prototype-based.

The Util.inherits method is equivalent to the combination inheritance in native JS. Children can inherit only functions that are defined by the parent in the prototype, and the properties in the constructor are private and not inherited.

Two. 2 Util.inspect

Util.inspect (object, [ShowHidden], [depth], [colors]) is a method that converts any object to a string, typically for debugging and error output. Accept at least one parameter, object.

ShowHidden: Optional parameter, if true, will output more hidden information. The default is False.

Depth: The maximum number of recursive layers, if the object is complex, you can specify the number of layers to control how much output information, the default is 2 layers, set to null, that is, the full traversal of an unlimited number of recursive layers of the object.

Color: If true, the output format will be encoded in ANSI color, typically used to display a more beautiful effect on the terminal. The default is false, which displays normal colors.

Custominspect: If False, inspect (depth,opt) is not executed and is true by default.

Note: Util.inspect does not simply convert an object to a string, even if the object defines the ToString method.

node. JS Learning Note 5--Core Module 1

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.