编程爱好者联盟 2016-12-18
1.事件驱动程序绑定事件及事件的处理程序
eventEmitter.on('eventName', eventHandler); 我们可以通过程序触发事件
// 触发事件
eventEmitter.emit('eventName'); 2.实例
var EventEmitter = require('events').EventEmitter
var a = new EventEmitter;
//在Node.js中如何注册一个事件
a.on('myevent', function () {
console.log('event called......')
})
process.nextTick(function () {
a.emit('myevent')
})
console.log('oooooooo')

3.让类都具有事件的功能
app.js
var MyClass = require('./Test')
var aa = new MyClass
aa.on('fsfdfs',function(){
}) Test.js
var EventEmitter = process.EventEmitter
var MyClass = function () {
///........
}
MyClass.prototype.__proto__ = EventEmitter.prototype