Node. js Learning (9) ---- Core Module

Source: Internet
Author: User

 Global object;
Frequently used tools;
Failover event mechanism;
Token file system access;
 HTTP server and client.

Variables defined by the parent layer;
Attributes of the global object;
Implicit variables (undefined variables with direct values ).
When you define a global variable, this variable also becomes the attribute of the global object, and vice versa. Note that in Node. js, you cannot define variables in the outermost layer, because all user code belongs to the current module, and the module itself is not the outermost context.

Provides a simple interface with the operating system. When you write a local command line program, you need to deal with it. The following describes some of the most common member methods of the process object.

Prepare process. stdin is a standard input stream, which is paused at the beginning. to read data from the standard input, you must restore the stream and manually write the Event Response Function of the stream.

process.stdin.resume();process.stdin.on('data', function(data) {process.stdout.write('read from console: ' + data.toString());});
Callback process. nextTick (callback) is used to set a task for the event loop. Node. js will call callback when the next event loop is called.

Console. log ('Hello World ');

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

Var util = require ('til'); function Base () {this. name = 'base'; this. base = 1991; this. sayHello = function () {console. log ('hello' + this. name) ;}}// here is the Base function defined by the Base prototype. prototype. showName = function () {console. log (this. name) ;}; function Sub () {this. name = 'sub';} util. inherits (Sub, Base); var objBase = new Base (); objBase. showName (); objBase. sayHello (); console. log (objBase); var objSub = new Sub (); objSub. showName (); // objSub. sayHello (); console. log (objSub );
We have defined a Base object and a Sub that inherits from the Base. The Base has three properties defined in the constructor and a function defined in the prototype. inherits implements inheritance. The running result is as follows:

Hello base
{Name: 'base', base: 1991, sayHello: [Function]}
Sub
{Name: 'sub '}

String method, which is usually used for debugging and error output. It accepts at least one parameter object, that is, the object to be converted.
ShowHidden is an optional parameter. If it is set to true, more hidden information is output.
Depth indicates the maximum number of recursive layers. if the object is complex, you can specify the number of layers to control the output information. If depth is not specified, Layer 2 will be recursive by default. If it is null, the object will be completely traversed by no limit to the number of recursive layers.
If the color value is true, the output format is encoded in ANSI color, which is usually used to display more beautiful results on the terminal.
In particular, util. inspect does not simply convert an object to a string, even if the object defines the toString method.

var util=require('util');function Person(){this.name='MrGe';this.toString=function(){return this.name;};}var obj=new Person();console.log(util.inspect(obj));console.log(util.inspect(obj,true));
Result output:

{Name: 'mrge ',
ToString:
{[Function]
[Length]: 0,
[Name]: '',
[Arguments]: null,
[Caller]: null,
[Prototype]: {[constructor]: [Circular] }}}

Pass the callback function parameters.

var events=require('events');var emitter = new events.EventEmitter();emitter.on('someEvent',function(arg1,arg2){console.log('listener1',arg1,arg2);});emitter.on('someEvent',function(arg1,arg2){console.log('listener2',arg1,arg2);});emitter.emit('someEvent','Tome',1234567);
Running result:

Listener2 top me 1234567

EventEmitter. on (event, listener) registers a listener for the specified event and accepts a string event and a callback function listener.
EventEmitter. emit (event, [arg1], [arg2], [...]) transmits event Events and transmits several optional parameters to the parameter table of the event listener.
EventEmitter. once (event, listener) registers a single listener for a specified event. That is, the listener is triggered only once at most, and immediately releases the listener after the event is triggered.

var events = require('events');var emitter = new events.EventEmitter();emitter.emit('error');
3.3 inherit EventEmitter

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.