node. JS Learning Note 6

Source: Internet
Author: User

Event-driven events

Events is the most important module in node. js because the framework of node. JS itself is an event, and events provides a unique interface.

Event Launcher events. Eventemitter

Events. The core of Eventemitter is the encapsulation of event launch and event listener functionality. Each event of a eventemitter consists of an event name and several parameters, which is a string that usually expresses a certain semantics.

For each event, events. Eventemitter supports a number of event listeners. When an event is emitted, the event listener registered to the event is called sequentially, and the event arguments are passed as a callback function parameter.

Eventemitter.on (event, listener) registers a listener for the specified event, accepts a string event, and listener a callback function.

Eventemitter.emit (event, [Arg1],[arg2], [Arg3]..) Launch event events, passing the burst sense optional parameter to the event listener's parameter table.

Eventemitter.once (event, listener) registers a single listener for the specified event, that is, the listener is fired at most once, and the listener is dismissed as soon as it is triggered.

Eventemitter.removelistener (Event,listener) removes a listener for the specified event, listener must be a listener that the event has already registered.

Eventemitter.removealllistener ([Event]) removes the listener for all events and, if specified, removes all listeners for the specified event.

will return emitter to ensure chain operation.

Error Event

Eventemitter defines a special event error that contains the semantics of "error", and Eventemitter specifies that if there is no response listener, node. JS will take it as an exception, exit the program and print the call stack.

It is common to set the listener for the object that emits the error event, avoiding the entire program crashing after encountering an error.

Inherit Eventemitter

Most of the time we don't use eventemitter directly, but we inherit it from the object. Including Fs,net,http, as long as the core module that supports incident response is Eventemitter subclass.

The reason for this: one: An object that has an entity function implements the semantics of the event, and the listener and the launch should be the method of an object.

Two: JavaScript object mechanism is based on prototype, support partial multiple inheritance, inheritance Eventemitter will not disrupt the original inheritance relationship of the object.

node. JS Learning Note 6

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.