Home  >  Article  >  Web Front-end  >  Instructions for using the events.emitter.listeners method in node.js_node.js

Instructions for using the events.emitter.listeners method in node.js_node.js

WBOY
WBOYOriginal
2016-05-16 16:27:401405browse

Method description:

All listeners registered for the specified event will be returned as an array.

Grammar:

Copy code The code is as follows:

emitter.listeners(event)

Receive parameters:

event Specified event

Example:

Copy code The code is as follows:

server.on('connection', function (stream) {
console.log('someone connected!');
});
console.log(util.inspect(server.listeners('connection')));
// [[Function] ]

Source code:

Copy code The code is as follows:

EventEmitter.prototype.listeners = function(type) {
var ret;
if (!this._events || !this._events[type])
ret = [];
else if (util.isFunction(this._events[type]))
ret = [this._events[type]];
else
ret = this._events[type].slice();
Return ret;
};
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn