Object built-in object

ObjectEventEmitter

EventEmitter is an event triggering object, which can be used to establish the observer pattern. Objects that support event triggering all inherit from this

When an event is triggered, all listeners associated with the event are called asynchronously. It also allows us to create code that is highly customizable and flexible.

Commonly used functions include: addListener/on, once, removeListener/off, removeAllListeners and emit.

Here is a sample code:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
var fs = require('fs'); var EventEmitter = require('events'); var event = new EventEmitter(); event.on('read_file', function(filename) { fs.readFile(filename, 'utf8', function(err, data) { if (err) { event.emit('error', err); return; } event.emit('show_content', data); }); }); event.on('error', function(err) { console.log(`Error ${err}`); }); event.on('show_content', function(content) { console.log(content); }); event.emit('read_file', 'test.txt');

event.emit('read_file', 'test.txt')When the above sample code is run, the event emitter instance event first listens to the 'read_file' event, and then triggers the operation of reading the file when the event is triggered ( ). When the reading is successful, the 'show_content' event will be triggered. At this time, the function that listened to the 'show_content' event will be executed and the file content will be displayed. If an error occurs while reading the file, the 'error' event is triggered, and the failure of the operation is handled.

This model has great advantages in business scenarios dealing with asynchronous operations.

inheritance relationship

%0 object object toString() toJSON() EventEmitter EventEmitter new EventEmitter() EventEmitter defaultMaxListeners on() addListener() prependListener() once() prependOnceListener() off() removeListener() removeAllListeners() setMaxListeners() getMaxListeners() listeners() listenerCount() eventNames() emit() object->EventEmitter ChildProcess ChildProcess EventEmitter->ChildProcess DgramSocket DgramSocket EventEmitter->DgramSocket FSWatcher FSWatcher EventEmitter->FSWatcher LruCache LruCache EventEmitter->LruCache Service Service EventEmitter->Service StatsWatcher StatsWatcher EventEmitter->StatsWatcher WebSocket WebSocket EventEmitter->WebSocket WebView WebView EventEmitter->WebView Worker Worker EventEmitter->Worker

Constructor

EventEmitter

Constructor

1
new EventEmitter();

object

event trigger object

1
EventEmitter new EventEmitter;

static properties

defaultMaxListeners

Integer, the default global maximum number of listeners

1
static Integer EventEmitter.defaultMaxListeners;

member function

on

Bind an event handler to the object

1 2
Object EventEmitter.on(String ev, Function func);

Call parameters:

  • ev: String, specifies the name of the event
  • func: Function, specify the event processing function

Return results:

  • Object, returns the event object itself to facilitate chain calls

Bind an event handler to the object

1
Object EventEmitter.on(Object map);

Call parameters:

  • map: Object, specifies the event mapping relationship, the object attribute name will be used as the event name, and the value of the attribute will be used as the event processing function

Return results:

  • Object, returns the event object itself to facilitate chain calls

addListener

Bind an event handler to the object

1 2
Object EventEmitter.addListener(String ev, Function func);

Call parameters:

  • ev: String, specifies the name of the event
  • func: Function, specify the event processing function

Return results:

  • Object, returns the event object itself to facilitate chain calls

Bind an event handler to the object

1
Object EventEmitter.addListener(Object map);

Call parameters:

  • map: Object, specifies the event mapping relationship, the object attribute name will be used as the event name, and the value of the attribute will be used as the event processing function

Return results:

  • Object, returns the event object itself to facilitate chain calls

prependListener

Bind an event handler to the object's origin

1 2
Object EventEmitter.prependListener(String ev, Function func);

Call parameters:

  • ev: String, specifies the name of the event
  • func: Function, specify the event processing function

Return results:

  • Object, returns the event object itself to facilitate chain calls

Bind an event handler to the object's origin

1
Object EventEmitter.prependListener(Object map);

Call parameters:

  • map: Object, specifies the event mapping relationship, the object attribute name will be used as the event name, and the value of the attribute will be used as the event processing function

Return results:

  • Object, returns the event object itself to facilitate chain calls

once

Bind a one-time event handler to the object. The one-time handler will only be triggered once.

1 2
Object EventEmitter.once(String ev, Function func);

Call parameters:

  • ev: String, specifies the name of the event
  • func: Function, specify the event processing function

Return results:

  • Object, returns the event object itself to facilitate chain calls

Bind a one-time event handler to the object. The one-time handler will only be triggered once.

1
Object EventEmitter.once(Object map);

Call parameters:

  • map: Object, specifies the event mapping relationship, the object attribute name will be used as the event name, and the value of the attribute will be used as the event processing function

Return results:

  • Object, returns the event object itself to facilitate chain calls

prependOnceListener

Bind an event handler to the object's origin

1 2
Object EventEmitter.prependOnceListener(String ev, Function func);

Call parameters:

  • ev: String, specifies the name of the event
  • func: Function, specify the event processing function

Return results:

  • Object, returns the event object itself to facilitate chain calls

Bind an event handler to the object's origin

1
Object EventEmitter.prependOnceListener(Object map);

Call parameters:

  • map: Object, specifies the event mapping relationship, the object attribute name will be used as the event name, and the value of the attribute will be used as the event processing function

Return results:

  • Object, returns the event object itself to facilitate chain calls

off

Unassign a function from the object processing queue

1 2
Object EventEmitter.off(String ev, Function func);

Call parameters:

  • ev: String, specifies the name of the event
  • func: Function, specify the event processing function

Return results:

  • Object, returns the event object itself to facilitate chain calls

Cancel all functions in the object processing queue

1
Object EventEmitter.off(String ev);

Call parameters:

  • ev: String, specifies the name of the event

Return results:

  • Object, returns the event object itself to facilitate chain calls

Unassign a function from the object processing queue

1
Object EventEmitter.off(Object map);

Call parameters:

  • map: Object, specifies the event mapping relationship, the object attribute name is used as the event name, and the value of the attribute is used as the event processing function

Return results:

  • Object, returns the event object itself to facilitate chain calls

removeListener

Unassign a function from the object processing queue

1 2
Object EventEmitter.removeListener(String ev, Function func);

Call parameters:

  • ev: String, specifies the name of the event
  • func: Function, specify the event processing function

Return results:

  • Object, returns the event object itself to facilitate chain calls

Cancel all functions in the object processing queue

1
Object EventEmitter.removeListener(String ev);

Call parameters:

  • ev: String, specifies the name of the event

Return results:

  • Object, returns the event object itself to facilitate chain calls

Unassign a function from the object processing queue

1
Object EventEmitter.removeListener(Object map);

Call parameters:

  • map: Object, specifies the event mapping relationship, the object attribute name is used as the event name, and the value of the attribute is used as the event processing function

Return results:

  • Object, returns the event object itself to facilitate chain calls

removeAllListeners

Cancels all listeners for all events from the object's processing queue. If an event is specified, removes all listeners for the specified event.

1
Object EventEmitter.removeAllListeners(String ev);

Call parameters:

  • ev: String, specifies the name of the event

Return results:

  • Object, returns the event object itself to facilitate chain calls

Cancels all listeners for all events from the object's processing queue. If an event is specified, removes all listeners for the specified event.

1
Object EventEmitter.removeAllListeners(Array evs = []);

Call parameters:

  • evs: Array, specify the name of the event

Return results:

  • Object, returns the event object itself to facilitate chain calls

setMaxListeners

The default limit on the number of listeners, for compatibility only

1
EventEmitter.setMaxListeners(Integer n);

Call parameters:

  • n: Integer, specify the number of events

getMaxListeners

Gets the default limit number of listeners, for compatibility only

1
Integer EventEmitter.getMaxListeners();

Return results:

  • Integer, returns the default limit quantity

listeners

Query the listener array for the specified event of the object

1
Array EventEmitter.listeners(String ev);

Call parameters:

  • ev: String, specifies the name of the event

Return results:

  • Array, returns the listener array for the specified event

listenerCount

Query the number of listeners for the specified event of the object

1
Integer EventEmitter.listenerCount(String ev);

Call parameters:

  • ev: String, specifies the name of the event

Return results:

  • Integer, returns the number of listeners for the specified event

Query the number of listeners for the specified event of the object

1 2
Integer EventEmitter.listenerCount(Value o, String ev);

Call parameters:

  • o: Value, specifies the object of the query
  • ev: String, specifies the name of the event

Return results:

  • Integer, returns the number of listeners for the specified event

eventNames

Query listener event name

1
Array EventEmitter.eventNames();

Return results:

  • Array, returns an array of event names

emit

Actively trigger an event

1 2
Boolean EventEmitter.emit(String ev, ...args);

Call parameters:

  • ev: String, event name
  • args: ..., event parameters will be passed to the event processing function

Return results:

  • Boolean, returns the event trigger status, returns true if there is a response event, otherwise returns false

toString

Returns the string representation of the object. Generally, "[Native Object]" is returned. The object can be re-implemented according to its own characteristics.

1
String EventEmitter.toString();

Return results:

  • String, returns the string representation of the object

toJSON

Returns a JSON format representation of the object, generally returning a collection of readable properties defined by the object.

1
Value EventEmitter.toJSON(String key = "");

Call parameters:

  • key: String, not used

Return results:

  • Value, returns a value containing JSON serializable