Original title: node. js Eventemitter Tutorial
Original link: http://www.hacksparrow.com/node-js-eventemitter-tutorial.html
Ever heard of node. js EventEmitter
? Perhaps you know that most of the built-in node. JS libraries Use it, and perhaps you are always curious about it, EventEmitter
but who doesn't explain it to you?
In this tutorial, I'll give you some EventEmitter
examples of classes.
You have to be familiar with the various events in node. js such as: on data
, on end
, on error
etc. The event mechanism worked flawlessly and perfectly, didn't it? All of the event-based node. JS libraries are dependent on EventEmitter
classes, and you might say that those libraries are inherited EventEmitter
.
EventEmitter
The power of this is not limited to the built-in node. JS Library, you can have it!
The EventEmitter
best way to demonstrate your ability is to look at this in one example:
var EventEmitter = require(‘events‘).EventEmitter;var radium = new EventEmitter();//绑定 radiation 事件radium.on(‘radiation‘, function(ray) { console.log(ray);});setInterval(function() { //触发 radiation 事件 radium.emit(‘radiation‘, ‘GAMMA‘);}, 1000);
Notice how simple it is to create an event, add an event listener, trigger an event, and pass the data through the event. All of this is EventEmitter
achieved by magic. EventEmitter
make it possible to write amazing node. JS libraries.
The above example is based on an EventEmitter
instance, what can we do to create a class that inherits from EventEmitter
it? node. JS has a library called, which util
has a method: a inherits
function that implements the prototype inheritance between objects. It's easy for us to let a class inherit another class (not exactly called a Class):
var util = require(‘util‘);util.inherits(MyClass, SuperClass);
Using util.inherits()
, we created a EventEmitter
module that inherits from, note the following code:
Content in radio.js :
var util = require(‘util‘);var EventEmitter = require(‘events‘).EventEmitter;//一个包含 "freq" 和 "name" 属性的对象var Radio = function(station) { //保存 指向Radio的this,在setTimeout()中使用 var self = this; setTimeout(function() { self.emit(‘open‘, station); }, 0); setTimeout(function() { self.emit(‘close‘, station); }, 5000); this.on(‘newListener‘, function(listener) { console.log(‘Event Listener: ‘ + listener); });};//Radio 继承 EventEmitterutil.inherits(Radio, EventEmitter);module.exports = Radio;
We have created a module that inherits from it EventEmitter
. Now let's look at how to use the created module in one example.
Content in example.js :
var Radio = require(‘./radio.js‘);var station = { freq: ‘80.16‘, name: ‘Rock N Roll Radio‘,};// 创建一个Radio实例var radio = new Radio(station);//添加一个“open”事件监听器radio.on(‘open‘, function(station) { console.log(‘"%s" FM %s OPENED‘, station.name, station.freq); console.log(‘? ??‘);});//添加一个“close”事件监听器radio.on(‘close‘, function(station) { console.log(‘"%s" FM %s CLOSED‘, station.name, station.freq);});
Run Example.js ready to witness EventEmitter
the magic.
If for some reason you don't want to use util
a module, you can use this method to extend a class.
Apple.prototype = Object.create(require(‘events‘).EventEmitter.prototype);
EventEmitter
can help you write an impressive event-based node. JS module. At the same time, as a developer of node. js, the knowledge you have about EventEmitter
will greatly affect your productivity. So make sure you read EventEmitter
more detailed information about it and how it works in and out. If you don't understand EventEmitter
, then you don't know nodejs.js.
Finish
The level of translation needs to be improved, the translation of the blog is not according to the original sentence of a sentence translated, but added their own understanding of the article. If there is an incorrect place, please correct me! Personal notes, for informational purposes only.
Reference: http://www.hacksparrow.com/node-js-eventemitter-tutorial.html